我想添加一个行为扩展,以便我的服务类将使用Ninject进行实例化。我创建了一个继承自BehaviorExtensionElement的类,并在我的App.config中注册它。我看不到任何明显的东西,我错过了,但在启动时却被抛出:
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
这是我的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>
这是我的行为类:
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();
}
}
答案 0 :(得分:20)
原来,类型名称必须完全等同于typeof(DependencyInjectionServiceBehavior).AssemblyQualifiedName
。我之前的理解是,完全限定的类型名称中不应该有任何空格,结果证明是错误的。