【问题标题】:WCF error: extension could not be loadedWCF 错误:无法加载扩展
【发布时间】:2012-06-02 23:27:13
【问题描述】:

我在我的项目中定义了一个覆盖IDispatchMessageInspector 的类,我添加了相关的配置,但它不起作用

System.Configuration.ConfigurationErrorsException:无法加载为扩展“customHeaders”注册的类型“InMotionGIT_NT.Address.Service、CustomHeaders、Version=1.0.0.0、Culture=neutral、PublicKeyToken=null”。 (C:\Users\jmachado\Documents\Visual Studio 2010\Projects\InMotionGIT_NT\Address Service\InMotionGIT_NT.Address.Service\bin\Debug\InMotionGIT_NT.Address.Service.dll.config 第 67 行)

这就是我调用自定义扩展的方式

<endpointBehaviors>
    <behavior name="jsonBehavior">
        <enableWebScript/>
        <customHeaders/>
        <!--<webHttp/>-->
    </behavior>
</endpointBehaviors>    

这就是我定义自定义扩展的方式

<behaviorExtensions>
    <add name="customHeaders" type="InMotionGIT_NT.Address.Service, CustomHeaders, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>

这是我定义的类,在我的项目中

[AttributeUsage(AttributeTargets.Class)]
public class CustomHeaders : IDispatchMessageInspector
{
    public object AfterReceiveRequest(ref Message request, ClientChannel channel, InstanceContext instanceContext)
    {
        if ((WebOperationContext.Current.IncomingRequest.Method == "GET"))
        {
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "POST");
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept");
        }
        return null;
    }

    public void BeforeSendReply(ref Message reply, object correlationState)
    {
    }
}

我是否遗漏了配置中的某些内容?

【问题讨论】:

    标签: c# .net wcf exception-handling wcf-extensions


    【解决方案1】:

    更改您的类型定义。首先,指定完整的类型名称(命名空间 + 类名)。在逗号之后,放置包含您的类型的 DLL 的名称。然后剩下的就是一个完全限定的类型名称。像这样:

    <behaviorExtensions>
        <add name="customHeaders" type="InMotionGIT_NT.Address.Service.CustomHeaders, <DLLName> , Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </behaviorExtensions>
    

    【讨论】:

    • 请注意,在许多情况下(例如,私有类型),您不需要完全限定名称,即只需执行 My.Namespace.MyType, MyDll 可能就足够了。
    【解决方案2】:

    确保版本与 dll 版本相同。在我的情况下,我引用了这些类所属的相同的 asssemlby。但是我在 AssemlbyInfo.cs 文件中更改了与 App.config 文件中的版本不匹配的程序集版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      • 2017-11-25
      • 2015-04-09
      • 2018-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多