gpt4 book ai didi

c# - 奇怪的异常场景 System.Net.Http MediaTypeHeaderValue 无法转换为 MediaTypeHeaderValue

转载 作者:行者123 更新时间:2023-11-30 13:31:59 25 4
gpt4 key购买 nike

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

[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 包作为依赖项,即使在下拉菜单中选择了“Stable Only”也是如此。

最佳答案

事实证明,某些 NuGet 更新出现了严重错误,它在客户端的我的 web.config 文件中创建了恶意绑定(bind)重定向。

  <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>

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

关于c# - 奇怪的异常场景 System.Net.Http MediaTypeHeaderValue 无法转换为 MediaTypeHeaderValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16967134/

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