views:

1146

answers:

1

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?


Update

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.

I've uploaded a minimal sample project where the bug occurs to megaupload.

+2  A: 

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.

tomasr
In general, what you're doing should work. Have you tried it without using WebServiceHost, but the normal one?
tomasr
Yes, tried it also with a ServiceHost. Problem persists. I've noticed that the error message doesn't change even if I change the "type" attribute in the extension to something nonsense like "foobar". Additionaly, I've tried adding the binding's dll into the global assembly cache. Error message still the same :-(
Etan
Sounds like your binding extension is not getting loaded at all then. Have you tried using fuslogvw.exe to check if perhaps the assembly is not getting loaded? WCF is picky about how the assembly name is written when registering config extensions.
tomasr
Fuslogvw.exe didn't help (stayed empty regardless of what I've entered as type). However, including what's said http://blogs.msdn.com/markgabarra/archive/2006/04/27/585607.aspx creates error messages if it can't load properly. When I enter the correct name, it loads up and shows in the console my binding, but my problem persists when I try to create the `ServiceHost`.
Etan
Thanks! Never would have come to THAT solution from my own!
Etan