Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / XML

WCF Custom Binding

4.57/5 (7 votes)
21 Feb 2013CPOL 86.5K  
In WCF, you can easily create custom bindings using configuration and custom implementations.

Introduction

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

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

Option 2: Programmatically

C#
// 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 = 
         host.AddServiceEndpoint(typeof(IHelloService),
                           myBinding,
                           "http://localhost:8080/HelloService");
                           
// service is ready for user
host.Open();

Option 3: Custom Implementation

C#
public class MyCustomBinding : Binding
{
    private HttpTransportBindingElement transport;
    private BinaryMessageEncodingBindingElement encoding;

    public MyCustomBinding()
           : base()
    {
      this.InitializeValue();
    }
    public override BindingElementCollection CreateBindingElements()
    {
      BindingElementCollection elements = new BindingElementCollection();
      elements.Add(this.encoding);
      elements.Add(this.transport);
      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:

C#
public class MyCustomBindingCollectionElement 
        : BindingCollectionElement
{
    // type of custom binding class
    public override Type BindingType
    {
      get { return typeof(MyCustomBinding); }
    }
    
    // override ConfiguredBindings
    public override ReadOnlyCollection<IBindingConfigurationElement>
                          ConfiguredBindings
    {
      get
      {
        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

XML
<system.serviceModel>
    <services>
        <service name="WcfSamples.HelloService">
            <endpoint address="net.tcp://localhost:10101/IHelloService"
            binding=""myCustomBinding""
            contract="WcfSamples.IHelloService">
            </endpoint>
        </service>
    </services>
    <extensions>
        <bindingExtensions>
            <add name="myCustomBinding"
               type="CustomBinding.MyCustomBindingCollectionElement,
               CustomBinding,
               Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
        </bindingExtensions>
    </extensions>
</system.serviceModel>

behaviors - Apply rules and behavior on a service

Conclusion

I hope you got some idea about WCF custom binding and its configuration. Thanks for reading Smile | <img src= " src="http://www.codeproject.com/script/Forums/Images/smiley_smile.gif" />

License

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