14

I want to add a behavior extension so that my service classes will be instantiated with Ninject. I created a class that inherits from BehaviorExtensionElement and registered it in my App.config. I cannot see anything obvious I'm missing, yet on startup this is thrown:

System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for system.serviceModel/behaviors: Extension element TestExtension cannot be added to this element. Verify that the extension is registered in the extension collection at system.serviceModel/extensions/behaviorExtensions.
Parameter name: element (...\MyAssembly.dll.config line 42) ---> 
System.ArgumentException: Extension element TestExtension cannot be added to this element. 
Verify that the extension is registered in the extension collection at system.serviceModel/extensions/behaviorExtensions.
Parameter name: element

Here is my App.config:

<system.serviceModel>
<extensions>
  <behaviorExtensions>
    <add name="TestExtension" type="Mynamespace.DependencyInjectionServiceBehavior,MyAssembly,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" />
  </behaviorExtensions>
</extensions>
<behaviors>
  <serviceBehaviors>
    <behavior name="MyServiceBehavior">
      <TestExtension/>
    </behavior>
  </serviceBehaviors>
</behaviors>

Here is my behaviour class:

public class DependencyInjectionServiceBehavior : BehaviorExtensionElement, IServiceBehavior
{
    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers)
        {
            ChannelDispatcher cd = cdb as ChannelDispatcher;
            if (cd != null)
            {
                foreach (EndpointDispatcher ed in cd.Endpoints)
                {
                    ed.DispatchRuntime.InstanceProvider =
                        new DependencyInjectionInstanceProvider(serviceDescription.ServiceType);
                }
            }
        }
    }

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
    }

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase,
        Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {
    }

    public override Type BehaviorType
    {
        get { return this.GetType(); }
    }

    protected override object CreateBehavior()
    {
        return new DependencyInjectionServiceBehavior();
    }

}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Blake Pettersson
  • 8,927
  • 3
  • 27
  • 36

1 Answers1

20

Turns out the type name needs to be exactly equivalent to typeof(DependencyInjectionServiceBehavior).AssemblyQualifiedName. My earlier understanding was that there wasn't supposed to be any spaces in the fully qualified type name, which turned out to be wrong.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Blake Pettersson
  • 8,927
  • 3
  • 27
  • 36
  • 9
    Using the WCF Service Configuration Editor to add in the behavior extension will allow you to browse for the DLL and class within and will add in the proper information to your configuration file as well. Just another alternative that might be a little easier for some people! –  Jun 29 '11 at 12:16
  • Check here http://stackoverflow.com/questions/10727512/web-config-error-system-servicemodel/10738691#10738691 – NoWar May 24 '12 at 13:42