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:
- basicHttpBinding: This binding works only on http protocol. Best use of this binding is to convert web-services in WCF.
- wsHttpBinding: This binding basically does what basicHttpBinding does, but difference is this binding implements ws-* specification. It means wsHttpBinding = basicHttpBinding + security.
- NetNamedPipeBinding: This binding provide cross process communication on the same machine. This binding does not work across machines.
- NetMsmqBinding: This binding use MSMQ as transport. This is required when we have implemented some cross machine environment communication.
- 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.
- IMathService.cs
- MathService.svc
Delete all code of IMathService and write the following code.
- using System.ServiceModel;
-
- namespace wcfService
- {
- [ServiceContract]
- public interface IMathService
- {
- [OperationContract]
- int AddTwoNo(int FirstNo, int Second);
-
- [OperationContract]
- int DivideTwoNo(int first, int second);
- }
- }
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:
- using System;
-
- namespace wcfService
- {
- public class MathService : IMathService
- {
- public int AddTwoNo(int FirstNo, int Second)
- {
- return FirstNo + Second;
- }
-
-
- public int DivideTwoNo(int first, int second)
- {
- return first / second;
- }
- }
- }
As earlier said default binding is basicHttpBinding. We can define binding in web.config file.
- <system.serviceModel>
- <services>
- <service name="wcfService.MathService" behaviorConfiguration="mexBehaviour">
- <endpoint address="AddTwoNo" binding="basicHttpBinding" contract="wcfService.IMathService"></endpoint>
- <endpoint address="AddTwoNo" binding="netTcpBinding" contract="wcfService.IMathService"></endpoint>
- <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
- <host>
- <baseAddresses>
- <add baseAddress="http://localhost:23793"/>
- <add baseAddress="net.tcp://localhost:23795"/>
- </baseAddresses>
- </host>
- </service>
- </services>
- <behaviors>
- <serviceBehaviors>
- <behavior name="mexBehaviour">
- <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
- <serviceDebug includeExceptionDetailInFaults="true"/>
- </behavior>
- </serviceBehaviors>
- </behaviors>
- <protocolMapping>
- <add binding="basicHttpsBinding" scheme="https" />
- </protocolMapping>
- <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
- </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.