יונ.13

מרכיבים של WCF

מרכיבים של WCF

ממה מורכב WCF

Data contract and Service contract

  1. //
  2. //A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged.
  3. //Examples from microsoft site:
  4.  
  5. [ServiceContract]
  6. public interface ISampleInterface
  7. {
  8. // No data contract is required since both the parameter
  9. // and return types are primitive types.
  10. [OperationContract]
  11. double SquareRoot(int root);
  12.  
  13. // No Data Contract required because both parameter and return
  14. // types are marked with the SerializableAttribute attribute.
  15. [OperationContract]
  16. System.Drawing.Bitmap GetPicture(System.Uri pictureUri);
  17.  
  18. // The MyTypes.PurchaseOrder is a complex type, and thus
  19. // requires a data contract.
  20. [OperationContract]
  21. bool ApprovePurchaseOrder(MyTypes.PurchaseOrder po);
  22. }
  23.  
  24.  
  25. namespace MyTypes
  26. {
  27. [DataContract]
  28. public class PurchaseOrder
  29. {
  30. private int poId_value;
  31.  
  32. // Apply the DataMemberAttribute to the property.
  33. [DataMember]
  34. public int PurchaseOrderId
  35. {
  36.  
  37. get { return poId_value; }
  38. set { poId_value = value; }
  39. }
  40. }
  41. }


WCF Hosting:

  1. using (ServiceHost serviceHost = new ServiceHost(typeof (CalculatorService))) {
  2. serviceHost.Open();
  3.  
  4. Console.WriteLine("The calculator service is ready.");
  5. Console.WriteLine("Press <ENTER> to terminate service.");
  6. Console.WriteLine();
  7. Console.ReadLine();
  8. }


Hosting configuration example:


  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3. <appSettings>
  4. </appSettings>
  5.  
  6. <system.serviceModel>
  7. <services>
  8. <service name="TestService.CalculatorService"
  9. behaviorConfiguration="CalculatorServiceBehavior">
  10. <endpoint address="CalculatorService"
  11. binding="basicHttpBinding"
  12. contract="TestService.ICalculator" />
  13.  
  14. <endpoint address="mex"
  15. binding="mexHttpBinding"
  16. contract="IMetadataExchange" />
  17.  
  18. <host>
  19. <baseAddresses>
  20. <add baseAddress="http://localhost:8000/"/>
  21. </baseAddresses>
  22. </host>
  23. </service>
  24. </services>
  25.  
  26. <behaviors>
  27. <serviceBehaviors>
  28. <behavior name="CalculatorServiceBehavior">
  29. <serviceMetadata httpGetEnabled="true"/>
  30. </behavior>
  31. </serviceBehaviors>
  32. </behaviors>
  33.  
  34. </system.serviceModel>
  35. </configuration>

Channel factory client:

  1. var channelFactory = new ChannelFactory<IOrderServiceChannel>("httpEndPoint");
  2. var channel = channelFactory.CreateChannel();
  3.  
  4. channel.PlaceOrder("Here are my order details...");
  5. channel.Close();


ClientBase (usable when: needed event-driven asynchronous model or remotely, when contracts not shared):

  1. using System;
  2. using System.ServiceModel;
  3. namespace MyMagazine.ServiceArticle
  4. {
  5. public class ProductClient
  6. : ClientBase<IProductBrowser>,
  7. IProductBrowser
  8. {
  9. #region IProductBrowser Members
  10. public ProductData GetProduct(
  11. Guid productID)
  12. {
  13. return Channel.GetProduct(productID);
  14. }
  15. public ProductData[] GetAllProducts()
  16. {
  17. return Channel.GetAllProducts();
  18. }
  19. public ProductData[] FindProducts(
  20. string productNameWildcard)
  21. {
  22. return Channel.FindProducts(
  23. productNameWildcard);
  24. }
  25. #endregion
  26. }
  27. }



תגיות:
שתף את הסיפור הזה:

תגובות(0)

השאירו תגובה

קפטצ'ה לא מתאימה

תגובה