How to create custom binding in WCF Services

Posted by in WCF category on for Beginner level | Points: 150 | Views : 26856 red flag

This article will explain creation of custom binding in WCF services.

How to create a custom binding in WCF services 

Hello all in this article, I will explain the steps for creating the custom binding in WCF. 

What is binding in WCF?

  1. In WCF binding determines how the service communicates with its client. How the end points communicates with each other. 
  2. WCF comes with several predefined bindings and used as per the requirement. 
  3. The bindings determines the how service is accessed. I.e. binding specifies, 
  • The protocol used to access the service, 
  • Encoding used for messages, 
  • Security, 
  • Transports methods, 
  • Communication Pattern, 
  • Reliability, 
  • Interoperability etc.    

Custom Binding

  1. Custom binding is required when standard bindings do not fulfill our requirements. 
  2. For creating custom binding only Transport and Encoding methods are required elements all other elements are optional. 
We can create custom binding using following two approaches 
  • Using the configuration file 
    • Create <customBinding> element in the <bindings> collection as follows : 
                        <binding name=”custombinding”>

  • Through Code
    1. Add reference to “System.ServiceModel.Channel”  namespace. 
    2. Create the instance of “BindingElementCollection” class. 
    3. Specify the transport and encoding methods (Since those are required. You can also specify optional elements like security) 
    4. Create the instance of “CustomBinding” by passing the instance of “BindingElementCollection” class. CustomBinding class available in System.ServiceModel.Channel namespace, used to create a custom binding from a set of binding element. 
    5. Finally specify this binding while adding endpoint to host instance. 
Following is the sample code:
using System.ServiceModel.Channel; 
//Crating instance of ServiceHost class
ServiceHost host = new ServiceHost(typeof(MyService));
//BindingElementCollection instance
BindingElementCollection bindingElementCollection =new BindingElementCollection();
//Encoding Method
bindingElementCollection.Add(new MtomMessageEncodingBindingElement());
//Transport Method
bindingElementCollection.Add(new TCPTransportBindingElement());
//CustomBinding instance
CustomBinding customBinding = new CustomBinding(bindingElementCollection);
//Adding endpoint with custom binding to host
host.AddServiceEndpoint(typeog(IMyService),customBinding, “http://MyServer/MyService/”); 

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Prasham Sabadra
Member Level:
Member Status: Member
Member Since: 7/30/2010 3:02:30 AM
Country: India
Thanks and Best Regards Prasham
Hi, I am very passionate about the latest technologies and software development. MCTS for SharePoint 2010, MCTS in WSS 3.0 and MCP for .NET Framework 2.0. Also keen interested in Microsoft SharePoint technology.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)