【问题标题】:Odd Exception Scenario System.Net.Http MediaTypeHeaderValue cannot be cast to MediaTypeHeaderValue奇怪的异常场景 System.Net.Http MediaTypeHeaderValue 无法转换为 MediaTypeHeaderValue
【发布时间】:2013-06-06 16:16:55
【问题描述】:

我刚刚开始收到这个奇怪的异常,我完全不确定如何继续前进并解决它。

[A]System.Net.Http.Headers.MediaTypeHeaderValue cannot be cast to [B]System.Net.Http.Headers.MediaTypeHeaderValue.

Type A originates from 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' in the context 'Default' at location 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Net.Http\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Net.Http.dll'.

Type B originates from 'System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' in the context 'Default' at location 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Net.Http\v4.0_2.0.0.0__b03f5f7f11d50a3a\System.Net.Http.dll'

错误发生在我在客户端的 WebAPI 中这一行:

var data = responsecontent.ReadAsAsync<List<MyClass>>().Result;

我已经在托管 API 客户端的网站和 API 解决方案中检查了对该 DLL 的每一个引用。它们都引用了完全相同的 dll:

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Net.Http.dll

我注意到 API 使用的是较新版本的 System.Net.Http.Formatting dll,但它的唯一次要版本增量,并且它作为对其他东西的依赖而更新,所以我不愿意尝试“降级”并创建另一个过程中的问题。

API System.Net.Http.Formatting 是:

\Microsoft.AspNet.WebApi.Client.4.1.0-alpha-121112\lib\net40\System.Net.Http.Formatting.dll

网站 System.Net.Http.Formatting 是:

\Microsoft.AspNet.WebApi.Client.4.0.30506.0\lib\net40\System.Net.Http.Formatting.dll

我确实发现 NuGet 将下载 alpha 包作为依赖项,即使在下拉菜单中选择了“仅稳定”。

【问题讨论】:

    标签: c# asp.net .net asp.net-mvc-4 nuget


    【解决方案1】:

    事实证明,一些 NuGet 更新出了点问题,它在客户端的我的 web.config 文件中创建了一个恶意绑定重定向。

      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
    

    改回以前的版本:

      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
    

    修复了客户端的所有问题。

    【讨论】:

    • 感谢您回来回答这个问题。
    • 我遇到了同样的问题,但在尝试您的解决方案时遇到了这个问题:Could not load file or assembly 'System.Web.Http.WebHost, Version=4.0.0.0 有什么想法吗?
    • 检查该 dll 的重定向。可能是它是必需的,但不存在或设置错误。
    猜你喜欢
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    • 1970-01-01
    相关资源
    最近更新 更多