Introduction of WCF Bindings
Hi friends after basic difference of WCF & ASP.Net WebAPI today we will move ahead to know the binding and their importance in WCF. When we talk about WCF, binding play the important role to select specific WCF service in our application. If we want to use WCF, should have to identify the all types of bindings in WCF. All bindings are designed to fulfil some specific need. Even we can defined own custom binding to achieve specific requirement.
All in-built bindings are defined in the System.ServiceModel Namespace. Today we will discuss all pre-defined bindings in WCF. There are 10 types of bindings available in WCF.
Look at the following bindings in WCF:
1. Basic binding
- This binding is provided by the BasicHttpBinding Class.
- It is designed to expose a WCF service as an ASMX web service, so that old clients (which are still using ASMX web service) can consume new service.
- By default, it uses Http protocol for transport and encodes the message in UTF – 8 text for-mat. You can also use Https with this binding.
2. Web binding
- This binding is provided by the WebHttpBinding Class.
- It is designed to expose WCF services as Http requests by using HTTP-GET, HTTP-POST.
- It is used with REST based services which may give output as an XML or JSON format.
- This is very much used with social networks for implementing a syndication feed.
3. Web Service (WS) binding
- This binding is provided by the WSHttpBinding Class. It is like as Basic binding and uses Http or Https protocols for transport.
- But this is designed to offer various WS – * specifications such as WS – Reliable Messaging, WS – Transactions, WS – Security and so on which are not supported by Basic binding.
wsHttpBinding= basicHttpBinding + WS-* specification
4. WS Dual binding
- This binding is provided by the WsDualHttpBinding Class. It is like as wsHttpBinding except it sup-ports bi-directional communication means both clients and services can send and receive messages.
5. TCP binding
- This binding is provided by the NetTcpBinding Class.
- It uses TCP protocol for communication be-tween two machines with in intranet (means same network). It encodes the message in binary format.
- This is faster and more reliable binding as compared to the Http protocol bindings.
- It is only used when communication is WCF – to – WCF means both client and service should have WCF.
6. IPC binding
- This binding is provided by the NetNamedPipeBinding Class.
- It uses named pipe for Communication between two services on the same machine.
- This is the most secure and fastest binding among all the bindings.
7. MSMQ binding
- This binding is provided by the NetMsmqBinding Class. It uses MSMQ for transport and offers sup-port to disconnected message queued.
- It provides solutions for disconnected scenarios in which service processes the message at a different time than the client send the messages.
8. Federated WS binding
- This binding is provided by the WSFederationHttpBinding Class.
- It is a specialized form of WS binding and provides support to federated security.
9. Peer Network binding
- This binding is provided by the NetPeerTcpBinding Class. It uses TCP protocol but uses peer net-working as transport.
- In this networking each machine (node) acts as a client and a server to the other nodes. This is used in the file sharing systems like torrent.
10. MSMQ Integration binding
- This binding is provided by the MsmqIntegrationBinding Class.
- This binding offers support to communicate with existing systems that communicate via MSMQ.
In the above discussion we understood the basic concepts of bindings in WCF. Now move ahead and look the following image, where we will analyse and comparison all the above bindings to find best to fulfil our specific requirement. We all face the same problem to select WCF and their binding in our application. There are lots of scenario which help us to decide when we should go with WCF with specific binding.
Look at the following image:
After all the above discussion now move ahead to know "How to configure binding in our application?"
Binding can be configured either through Configuration file or Programming.
1. Configuration file:
In the configuration file of the hosting application, you can add the <bindings> element inside the <system.serviceModel> element and add the properties to particular binding type. Properties corresponding to the particular binding type can be mentioned below. Name of the binding properties that you are going to use has to be mention in the end point.
<system.serviceModel> <services> <service name="MyService"> <endpoint address="http://localhost/IISHostedService/MyService.svc" binding="wsHttpBinding" bindingName="wshttpbind" contract="IMyService"> <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <bindings> <wsHttpBinding> <binding name="wshttpbind" allowCookies="true" closeTimeout="00:01:00" receiveTimeout="00:01:00" /> </wsHttpBinding> </bindings> </system.serviceModel>
2. Code file :
In the following code, I have created the WSHttpBinding object and assign the properties which to be configured. This binding object is added to the Service endpoint for client communication. Similarly you can also create any type of binding and add to endpoint.
//Create a URI to serve as the base address Uri httpUrl = new Uri("http://localhost:8090/MyService/SimpleCalculator"); //Create ServiceHost ServiceHost host = new ServiceHost(typeof(MyCalculatorService.SimpleCalculator), httpUrl); //Create Binding to add to end point WSHttpBinding wshttpbind = new WSHttpBinding(); wshttpbind.AllowCookies = true; wshttpbind.CloseTimeout = new TimeSpan(0, 1, 0); wshttpbind.ReceiveTimeout = new TimeSpan(0, 1, 0); //Add a service endpoint host.AddServiceEndpoint (typeof(MyCalculatorService.ISimpleCalculator), wshttpbind, ""); //Enable metadata exchange ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; host.Description.Behaviors.Add(smb); //Start the Service host.Open(); Console.WriteLine("Service is host at " + DateTime.Now.ToString()); Console.WriteLine("Host is running... Press key to stop"); Console.ReadLine();
Note: It is always good if you configure the binding properties using configuration file, because while moving to the production you no need to change in the code and recompile it. It is always good practice to represent in the configuration file.
- Always create the service with Interface->Implementation format, mention the contract in Interface.
- Define the service in Class library and refer the class library in Host project. Don’t use service class in host project.
- Change the instance mode to per call as default.
- Always catch exception using try/catch block and throw exception using FaultException < T >.
- Logging and Include exception should be enable while compiling the project in debug mode. While in production deployment disable the logging and Include exception details.