Click here to Skip to main content
11,929,971 members (52,309 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


14 bookmarked

WCF Custom Binding

, 21 Feb 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
In WCF, you can easily create custom bindings using configuration and custom implementations.


In certain scenarios, you must create your own binding for additional transport protocols, security, etc. In WCF, you can easily create custom bindings using the configuration and custom implementations. Go through the following options for more information. This is the same as my original post WCF Custom Binding.

Option 1: Using Configuration

    <binding name="myCustomHttpBinding">
        <binaryMessageEncoder />
        <httpTransport / >
    </binding >    
</customBinding >

Option 2: Programmatically

// create the custom binding object
CustomBinding myBinding = new CustomBinding();

// add the custom binding elements
myBinding.Elements.Add(new BinaryMessageEncodingBindingElement());
myBinding.Elements.Add(new HttpTransportBindingElement());

// add the service endpoint
ServiceHost host = new ServiceHost(typeof(HelloService));
ServiceEndpoint serviceEndpoint = 
// service is ready for user

Option 3: Custom Implementation

public class MyCustomBinding : Binding
    private HttpTransportBindingElement transport;
    private BinaryMessageEncodingBindingElement encoding;

    public MyCustomBinding()
           : base()
    public override BindingElementCollection CreateBindingElements()
      BindingElementCollection elements = new BindingElementCollection();
      return elements;
    public override string Scheme
      get { return this.transport.Scheme; }
    private void InitializeValue()
      this.transport = new HttpTransportBindingElement();
      this.encoding = new BinaryMessageEncodingBindingElement();

If you want to use this custom binding via configuration, you must extend the BindingCollectionElement abstract base class as follows:

public class MyCustomBindingCollectionElement 
        : BindingCollectionElement
    // type of custom binding class
    public override Type BindingType
      get { return typeof(MyCustomBinding); }
    // override ConfiguredBindings
    public override ReadOnlyCollection<IBindingConfigurationElement>
        return new ReadOnlyCollection<IBindingConfigurationElement>(
        new List <IBindingConfigurationElement> ());
    // return Binding class object
    protected override Binding GetDefault()
       return new MyCustomBinding();

The property that you must implement is BindingType. It allows defining the binding type target of the current configuration. The other important property is ConfiguredBindings that retrieves all the binding configuration elements.

Using MyCustomBinding in the Configuration

        <service name="WcfSamples.HelloService">
            <endpoint address="net.tcp://localhost:10101/IHelloService"

            <add name=”myCustomBinding”


               Version=, Culture=neutral, PublicKeyToken=null” />

behaviors - Apply rules and behavior on a service


I hope you got some idea about WCF custom binding and its configuration. Thanks for reading Smile | <img src= " src="" />


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

A Mahesh
Web Developer
India India
Symphony Services, Banglore, 560 087, India

You may also be interested in...

Comments and Discussions

Generalrofl another copy paste from a half burnt foriegn sob Pin
ass nigger27-Aug-13 11:23
memberass nigger27-Aug-13 11:23 
QuestionExample? Pin
bbls198322-Jul-12 22:07
memberbbls198322-Jul-12 22:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 21 Feb 2013
Article Copyright 2011 by A Mahesh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid