gpt4 book ai didi

.net - 添加自定义 wcf 行为扩展会导致 ConfigurationErrorsException

转载 作者:行者123 更新时间:2023-12-03 00:30:14 24 4
gpt4 key购买 nike

我想添加一个行为扩展,以便我的服务类将使用 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();
}

}

最佳答案

事实证明,类型名称需要与 typeof(DependencyInjectionServiceBehavior).AssemblyQualifiedName 完全相同。我之前的理解是完全限定类型名称中不应该有任何空格,结果证明这是错误的。

关于.net - 添加自定义 wcf 行为扩展会导致 ConfigurationErrorsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1163996/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com