Etan
Etan

Reputation: 17554

Making a custom binding configurable over App.config

I've created a custom binding and want to make it configurable over App.config.

The binding has no special options at the moment, so it would be sufficient to support just

<endpoint address="http://myAddress" 
          binding="myBinding"
          contract="myContract">

After checking some sites, I found out that I have to enable configuration support through a <BindingExtension>. However, the MSDN site didn't help much, since when I try to add

<extensions>
  <bindingExtensions>
    <add name="myBinding" 
         type="MyNamespace.MyHttpBinding, NameOfMyDll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
  </bindingExtensions>
</extensions>

, I only receive the following error message when trying to launch the program:

Configuration binding extension 'system.serviceModel/bindings/myBinding' could not be found. Verify that this binding extension is properly registered in system.serviceModel/extensions/bindingExtensions and that it is spelled correctly.

The type mentioned in the bindingExtension points to the type which inherits from Binding.

What do I have to add to enable configuration support for my binding?


My goal is just to be able to export my binding to the config file. I don't want to allow any special settings for the binding. It should just be usable over the config file's <endpoint> tag.

Upvotes: 2

Views: 4387

Answers (1)

tomasr
tomasr

Reputation: 13849

You're on the right track. The key point, however, is that the bindingExtension element should not point directly to your binding class itself.

Instead, you need to have several class that support the configuration model. For starting, the bindingExtension you register is really a class that inherits from StandardBindingCollectionElement. This represents a collection of StandardBindingElement, which is the configuration class that has all the configuration properties that your binding will support in the .config file and would be responsible for creating your Binding instance and setting any properties on it that were set in the .config file.

Also, notice that, normally, you'd follow a similar pattern for creating a configuration view of your TransportBindingElement (if you're doing a transport channel) so that you can create custom bindings using it though configuration. In that case, you'd have a class inheriting TransportElement.

P.S. If you're thinking this is an awful lot of repetitive code if you've got lots of settings, then I agree.

Update: Found what your problem was: You need at least an empty <bindings/> section in your config file. Just add it and the binding will be recognized now.

Upvotes: 3

Related Questions