views:

1969

answers:

1

I've written a custom binding class that inherits from CustomBinding. My custom class overrides the BuildChannelFactory method and uses a custom ChannelFactory to create a custom channel.

I'm having difficulties using the custom binding class in the WCF client. I am able to use my custom binding class if I configure it in code:

Binding myCustomBinding = new MyCustomBinding();

ChannelFactory<ICustomerService> factory = 
   new ChannelFactory<ICustomerService>(myCustomBinding, 
        new EndpointAddress("http://localhost:8001/MyWcfServices/CustomerService/"));

ICustomerService serviceProxy = factory.CreateChannel();
serviceProxy.GetData(5);

My problem is I don't know how to configure it in the App.config file. Is it a customBinding element, or a bindingExtension element? Is it something else?

+3  A: 

When you created your custom binding in code, did you also implement a "YourBindingElement" (deriving from StandardBindingElement) and a "YourBindingCollectionElement" (deriving from StandardBindingCollectionElement) along with it?

If so - use that to configure your custom binding, as if it were any other binding.

The first step is to register your binding in the app.config or web.config file in the extensions section of <system.serviceModel>

<extensions>
  <bindingExtensions>
    <add name="yourBindingName" 
       type="YourBinding.YourBindingCollectionElement, YourBindingAssembly" />
  </bindingExtensions>
</extensions>

Now, your new binding is registered as a "normal" available binding in WCF. Specify your specifics in the bindings section as for other bindings, too:

<bindings>
  <yourBinding>
    <binding name="yourBindingConfig" 
             proxyAddress="...." useDefaultWebProxy="false" />
  </yourBinding>
</bindings>

Specify other parameters here, as defined in your "...BindingElement" class.

Lastly, use your binding like a normal binding in your services and/or client sections in system.serviceModel:

<client>
  <endpoint
    address="....your url here......"
    binding="yourBinding" 
    bindingConfiguration="yourBindingConfig"
    contract="IYourContract" 
    name="YourClientEndpointName" />
</client>

I couldn't really find a lot of resources on how to write your own binding in code on the web - Microsoft has a WCF/WPF/WF sample kit which includes a few samples from which I basically learned enough to figure it out :-)

There's one really good article by Michele Leroux Bustamante on creating your custom bindings - part 2 of a series, but part 1 is not available publicly :-(

Here's a sample custom binding in code for which you can download the complete source code: ClearUserNameBinding.

Marc

marc_s