This article will explain creation of custom binding in WCF services.
How to create a custom binding in WCF
Hello all in
this article, I will explain the steps for creating the custom binding in WCF.
What is binding in WCF?
- In WCF binding determines how the
service communicates with its client. How the end points communicates with each
- WCF comes with several predefined bindings and used as per the requirement.
- The bindings determines the how
service is accessed. I.e. binding specifies,
protocol used to access the service,
used for messages,
binding is required when standard bindings do not fulfill our requirements.
creating custom binding only Transport and Encoding methods are required
elements all other elements are optional.
create custom binding using following two approaches
- Using the configuration file
- Create <customBinding> element in the <bindings> collection as follows :
Following is the sample
- Add reference to “System.ServiceModel.Channel” namespace.
- Create the instance of “BindingElementCollection” class.
- Specify the transport and encoding
methods (Since those are required. You can also specify optional elements like
- 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.
- Finally specify this binding while
adding endpoint to host instance.
//Crating instance of ServiceHost class
ServiceHost host = new ServiceHost(typeof(MyService));
BindingElementCollection bindingElementCollection =new BindingElementCollection();
CustomBinding customBinding = new CustomBinding(bindingElementCollection);
//Adding endpoint with custom binding to host