WCF Programming Methods

WCF supports a diversity programming methods. This article discusses the three most common methods of developing WCF services.
  • Declarative
  • Explicit
  • Configuration
Declarative programming is accomplished via attributes. These attributes are used to define the contracts and behavior of the services. 
  1. [ServiceContract]  
  2. public interface IMyWCFService  
  3. {  
  4.         [OperationContract]  
  5.         string Operation1(string myvalue);  
  6. }  
  7. public class MyWCFService : IMyWCFService  
  8. {  
  9.         public string Operation1(string myvalue)  
  10.         {  
  11.         return "Hello: " + myvalue;  
  12.         }  
  13. }  
For declarative programming, the attributes are added as shown below with highlighting.
  1. [ServiceContract (SessionMode = SessionMode.Required)]  
  2. public interface IMyWCFService  
  3. {  
  4.         [OperationContract (IsOneWay = true)]  
  5.         string Operation1(string myvalue);  
  6. }  
  7. public class MyWCFService : IMyWCFService  
  8. {  
  9.         public string Operation1(string myvalue)  
  10.         {  
  11.         return "Hello: " + myvalue;  
  12.         }  
  13. }
Explicit programming lets you work directly with all the main classes and interfaces provided by the WCF object model. The following provides a simple example.
  1. class WCFApp  
  2. {  
  3.         static void Main()  
  4.         {  
  5.             Uri uri = new Uri("address path");  
  6.             AddressHeader ah = AddressHeader.CreateAddressHeader("Header Name""About header "null);  
  7.             EndpointAddress ea = new EndpointAddress(new Uri("service URL"), ah);  
  8.             ServiceHost sh = new ServiceHost(typeof("your service"), uri);  
  9.             sh.Description.Endpoints.Add(new ServiceEndpoint(ContractDescription.GetContract(typeof("contract")), new WSHttpBinding(), ea));  
  10.             sh.Open();  sh.Close();  
  11.     }  
  12. }  
Configuration based programming does not need a recompile of the service. Its a pure XML file.
  1. <?xml version="1.0"?>  
  2. <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">  
  3.     <system.serviceModel>  
  4.             <services>  
  5.                 <service name="MyWCFService" behaviorConfiguration="returnFaults">  
  6.                     <endpoint contract="IMyWCFService" binding="wsHttpBinding" address="http://localhost:1038/WCFDemoService/service.svc"></endpoint>  
  7.                 </service>  
  8.             </services>  
  9.             <behaviors>  
  10.                 <serviceBehaviors>  
  11.                     <behavior name="returnFaults">  
  12.                         <serviceMetadata httpGetEnabled="true"></serviceMetadata>  
  13.                     </behavior>  
  14.                 </serviceBehaviors>  
  15.             </behaviors>  
  16.     </system.serviceModel>  
  17.     <system.web>  
  18.             <compilation debug="true"/>  
  19.     </system.web>  
  20. </configuration>  
The best approach for developing a service is normally a combination of all three methods.
 
But we need to know how the execution order occurs.
 
First, attributes are applied. Second, configuration settings are applied.
 
This will override the attributes if there is a conflict.
 
Finally, the code is executed.
 
Hope this helps to clear up the basics of WCF Programming Methods.


Similar Articles