Binding In WCF: Part 11

Before reading this article, I highly recommend reading the previous parts of this article:
Before starting the example I am going to tell what binding is.
 

What WCF Binding is

 
Binding is a mechanism by which communication details are specified to make connection to a service endpoint. Each endpoint in WCF service requires a binding. Default binding is basicHttpBinding. Binding contain information about protocols, encoding and transport.
 
You can create your own custom binding as well. I will talk about this in later articles. According to MSDN, the most used common binding in WCF is:
  1. basicHttpBinding: This binding works only on http protocol. Best use of this binding is to convert web-services in WCF.

  2. wsHttpBinding: This binding basically does what basicHttpBinding does, but difference is this binding implements ws-* specification. It means wsHttpBinding = basicHttpBinding + security.

  3. NetNamedPipeBinding: This binding provide cross process communication on the same machine. This binding does not work across machines.

  4. NetMsmqBinding: This binding use MSMQ as transport. This is required when we have implemented some cross machine environment communication.

  5. WebBinding: This binding works only on http protocol. It is used when we try to implement restful services.
You can find more binding on MSDN link. It depends on our project requirement which binding suites my project. On behalf of that we choose the bindings. You can also create our own custom binding. For creating your own custom binding, click here.
 
Lets make a WCF application. In this application we are trying to add and divide numbers using WCF services. I am using Visual Studio 2012. Open Visual Studio and go to File, New, Project, then WCF and select WCF Service Application and name it wcfSevice.
 
Visual Studio provides some auto generated code. Let’s delete IService1.cs and Service1.svc file.
 
Add a wcf service file and name it MathService.svc and write the following code. It add the following two files.
  1. IMathService.cs
  2. MathService.svc
Delete all code of IMathService and write the following code.
  1. using System.ServiceModel;  
  2.   
  3. namespace wcfService  
  4. {  
  5.     [ServiceContract]  
  6.     public interface IMathService  
  7.     {  
  8.         [OperationContract]  
  9.         int AddTwoNo(int FirstNo, int Second);  
  10.   
  11.         [OperationContract]  
  12.         int DivideTwoNo(int first, int second);  
  13.     }  
  14. }  
ServiceContract describe which operation the client can perform on the service.
 
OperationContract define within a ServiceContract. It define the parameters and return type of operation.
 
Now implement the IMathService interface in MathService.svc.cs. Let’s delete all code first from MathService.svc.cs and write the following:
  1. using System;  
  2.   
  3. namespace wcfService  
  4. {  
  5.     public class MathService : IMathService  
  6.     {  
  7.         public int AddTwoNo(int FirstNo, int Second)  
  8.         {  
  9.             return FirstNo + Second;  
  10.         }  
  11.   
  12.   
  13.         public int DivideTwoNo(int first, int second)  
  14.         {  
  15.             return first / second;  
  16.         }  
  17.     }  
  18. }  
As earlier said default binding is basicHttpBinding. We can define binding in web.config file.
  1. <system.serviceModel>  
  2.     <services>  
  3.       <service name="wcfService.MathService" behaviorConfiguration="mexBehaviour">  
  4.         <endpoint address="AddTwoNo" binding="basicHttpBinding" contract="wcfService.IMathService"></endpoint>  
  5.      <endpoint address="AddTwoNo" binding="netTcpBinding" contract="wcfService.IMathService"></endpoint>  
  6.         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>  
  7.         <host>  
  8.           <baseAddresses>  
  9.             <add baseAddress="http://localhost:23793"/>  
  10.      <add baseAddress="net.tcp://localhost:23795"/>  
  11.           </baseAddresses>  
  12.         </host>  
  13.       </service>  
  14.     </services>  
  15.     <behaviors>  
  16.       <serviceBehaviors>  
  17.         <behavior name="mexBehaviour">  
  18.           <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>  
  19.           <serviceDebug includeExceptionDetailInFaults="true"/>  
  20.         </behavior>  
  21.       </serviceBehaviors>  
  22.     </behaviors>  
  23.     <protocolMapping>  
  24.         <add binding="basicHttpsBinding" scheme="https" />  
  25.     </protocolMapping>      
  26.     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />  
  27.   </system.serviceModel>  
In this example there are two bindings, one is basicHttpBinding and another is netTcpBinding. All bindings have their own property.
 
I hope you enjoyed this article.


Similar Articles