Keyvan Nayyeri

My daily musings about software and technology

Configuration Error for Custom Behavior Extensions in WCF

One of the very weird and not acceptable issues that you may face with during your development with Windows Communication Foundation is what I'm going to write about it here.

There is no doubt that you need to do some customizations in WCF for in-depth scenarios and one of the cases may be extending behaviors with custom behavior extensions.

Developing a custom behavior extension is a separate topic for its own but there is a very weird issue in configuration for a custom behavior extension.

You configure a WCF service for custom behavior extensions by adding a new element to sub-element located inside element within . Here all you need to do is choosing a name and setting the type for the behavior extension. Later you need to use this custom behavior in your configuration file.

Here is a sample configuration for such an extension:

<extensions>

  <behaviorExtensions>

    <add name="customHttpBehavior" type="SampleService.CustomBehavior, SampleService" />

  behaviorExtensions>

extensions>

Now if you try to get access to this service with this configuration then you should get a configuration error like this:

Configuration Error

Here is the error description:

An error occurred creating the configuration section handler for system.serviceModel/behaviors: Extension element 'customHttpBehavior' cannot be added to this element.  Verify that the extension is registered in the extension collection at system.serviceModel/extensions/behaviorExtensions.

This configuration error occurs whenever .NET runtime tries to parse your configuration and reaches to a custom behavior element that is defined by your extension and references it. As .NET is unable to load your behavior extension (due to the error that I describe) then it is unable to find your element and throws this configuration exception.

Now what's the reason? The reason is very bad for a product in this level (and many other guys have confirmed that). Mitch Denny has an excellent write up on this and there is a Microsoft Connect feedback item that reports this to MSFT. You can reach Mitch's post about the internal reasons to see this weird behavior from WCF and test it yourself.

In short, I would say that WCF has a built-in issue that doesn't let it load your assembly with the above definition (just by pointing to the type name and assembly name). Instead, you need to use the fully qualified name of your assembly and this will solve the issue for you.

So with something like the below configuration, you should be able to go on:

<extensions>

  <behaviorExtensions>

    <add name="customHttpBehavior" type="SampleService.CustomBehavior, SampleService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />

  behaviorExtensions>

extensions>

I hope that Microsoft solves this in the first service pack. Fortunately I was sure about the correctness of my code and configuration and quickly jumped into Google to search for the issue but as some people have written already, this may take a lot of time from someone who thinks that this is a problem with his or her code.

9 Comments

morrones
Aug 07, 2008 3:42 PM
#

Be aware that the example that the fully qualified name should be the exact same than the one of the assembly. In other words, an extra blank space would throw the same error.

Regards


Mak
Dec 31, 2008 8:02 PM
#

Despite of making above mentioned changes I am still getting same error, here is how my Extension element looks like

<extensions>

<behaviorExtensions>

<add name="AIMSAgentMessageInspecter" type="AIMSAgent.MessageInspecter.AIMSBehaviorExtensionElement,AIMSAgent.MessageInspecter,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" />

</behaviorExtensions>

</extensions>


Tim
Jan 13, 2010 10:43 AM
#

Thanks for the information. I was stuck on this for a couple hours. It seems like a pretty easy fix for MS. Hopefully they'll get this taken care of...


Menard
Feb 04, 2010 9:50 AM
#
You workaround will not work if the assembly is versioned or strong named

Krishna
Jun 22, 2010 3:38 AM
#
I do not get any configuration error but when i try to throw any error form client, i get the same fault message that is thrown from the client it does not create the fault message that is created in ProvideFault method. Any help is appreciated.

Thanks,
Krishna

Babu
Sep 04, 2010 11:43 AM
#
Hi, I added the Endpoint bavior extension as you suggested. Everything working fine in my development machine. Then i hosted the application in Server. Server is in Network Balanced Environment(NLB). Now i am getting the following error,

I am getting in SVC log is:

An extension of name 'MyServiceBehavior' already appears in extension collection. Extension names must be unique.

System.ServiceModel.Configuration.ExtensionElementCollection.EnforceUniqueElement(ExtensionElement element)

So please help me i am in the hectic situation.

Why its raising this error in NLB. How can i mitigate?

Toraj
Sep 13, 2010 3:13 PM
#
Hi Keyvan:
I truly enjoy your writings.
I have an existing WF using WCF, C#, VS2008 and IIS6. I have done WF self hosting and works just fine.
I migrated the same code to WF using WCF, C#, VS2010 and IIS7.0. When I select my project Service, Right Mouse Click, Update Service Reference, I get the following error.
“An error occurred creating the configuration section handler for system.serviceModel/behaviors: Extension element ’webHttp’ cannot be added to this element. Verify that the extension is registered in the extension collection at the system.serviceModel/extensions/behaviorExtensions. Parameter name; element app.config ln 150.”

I enclose the app.config. This same code base works fine in VS2008 and pointing to IIS 6 WS. Appreciate your help.

Regards, Toraj

Praveen
Apr 19, 2012 3:53 PM
#
Here is the solutions guys. Just create a seperate lbrary project and refer it in the config just like namespace.classname,assembly. It works very well

Sumit
Feb 07, 2013 5:37 AM
#
Thanks a lot. It resolved the issue.

Leave a Comment