RANT: An error occured creating the configuration section handler for system.serviceModel/behaviors…

Are you developing an extension to Windows Communication Foundation (for example a behavior or message encoder) with configuration file support? If you are you may be getting frustraited by an exception something like the following (the precise nature depends on the kind of extension you are developing).

I was, and the frustraiting thing was that I had built some of these in the past for various purposes without any issues so I had a real tough time figuring out what the hell was going on. Well it turns out that it is a string comparison issue. Lets take a look at the configuration file for a WCF service:

In this configuration file I have a simple math service but I have applied an endpoint behavior to it called “beep” in the configuration file. This is a custom behavior so I’ve had to implement my own BehaviorExtensionElement and in this case I’ve implemented the element and the actual IEndpointBehavior in the same class (because I can). What happens is when WCF initialises it loads in the list of the extensions into a hashtable (simplication – there is a lot more code involved than just a hashtable-like data structure) – so in this case “beep” is mapped to “ServerApplication.BeepBehavior….”, at the same time an instance of the class specified in the second value is instansiated and stored.

As execution continues and the service host is brought online the <beep /> element is encounted. The aforementioned hashtable is looked up and the fully qualified class name is retreived. This is then used as a key to find the custom BehaviorExtensionElement and this is where it comes unstuck. When the BehaviorExtensionElement is stored it is indexed with the fully qualified type name which is fetched using .GetType(), when this value is rendered as a string it looks like this:

ServerApplication.BeepBehavior, ServerApplication, Version=, Culture=neutral, PublicKeyToken=null

Notice the spaces between each of the type name elements. Now – compare this to the screenshot of the configuration file above. As you can see there is no space between the type name elements. Since the lookup is based on a simple string comparison the space is significant and the WCF runtime can’t find the previously created BehaviorExtensionElement instance and it all falls in a heap – if you go and put the spaces in it works perfectly.

The frustraiting thing is that the space sensitivity of the type attribute in the WCF portions of the configuration files is completely at odds with the norms in the rest of the .NET Framework (although I am aware of some similar issues in WPF thanks to Darren). Unfortunately it looks like Tomas Restrepo found the issue as well in this feedback entry on Microsoft Connect – but it looks like all development was closed off and it obviously shipped with this quirk.

Personally I think that this needs to be treated as a bug and fixed as soon as possible because it is going to drive developers who try to extend WCF completely nuts and when they realise what the problem was the WCF team better hope they are no where within reach. Given it isn’t a critical bug I suspect that it may have to wait for a service pack to be issued though.

10 thoughts on “RANT: An error occured creating the configuration section handler for system.serviceModel/behaviors…

  1. Andrew Parsons

    They shouldn’t be doing a complete string match anyhow. What if you wanted to put the attributes in a different order? It should properly parse the parameters out.

  2. Jowen

    Very good article.

    I’m having the same kind of problem trying to get a working bindingElementExtension (for using compression).
    I got real hopeful after reading this post, and I want to check whether or not it applies to my situation. Can you tell me how I can check the fully qualified type name (where to calll GetType() or how to view the hashtable)?

    Note: I’m using a proxy, so there is not a lot of code I can step into…


  3. Mitch Denny Post author

    Hi Jowen, basically you just do this:

    Type t = instance.GetType();

    Then just put that in your configuration file entries.

  4. Pingback: Configuration Error for Custom Behavior Extensions in WCF : Keyvan Nayyeri

  5. Pingback: Exception when not fully qualifying Behaviour Extensions « Dave Jansen’s Blog

  6. Shane Courtrille

    A better version of Mitch’s way of finding the Type name is..

    Type t = typeof(YourClass);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s