System.Net.Http NuGet 包 4.3.0 参考在 System.Diagnostics.DiagnosticSource 版本 4.0.0.0 参考上生成 System.IO.FileLoadException

System.Net.Http NuGet package 4.3.0 reference generates System.IO.FileLoadException on System.Diagnostics.DiagnosticSource ver 4.0.0.0 reference

问题描述:

共享库 "shared.dll" 项目引用 System.Net.Http NuGet 包 4.3.0。引用 "shared.dll" 的应用程序因

而失败

System.IO.FileLoadException

Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

at System.Net.Http.WinHttpHandler.SendAsync(...)

调查此问题后,我们发现上述失败的原因如下:

有几个解决方法:

  1. app.config 选项:为 any 声明 app.config 中的 binding redirection 的兼容版本(我们应该用它走多远?) =70=] - 引用应用程序。
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
  </dependentAssembly>
</assemblyBinding>
  1. 强制System.Diagnostics.DiagnosticSource.dll 到版本 4.0.0.0: 将对 System.Diagnostics.DiagnosticSource v. 4.0.0.0 的 NuGet 引用添加到引用 System.Net.Http 的项目中,以抢占 dll 版本 4.0.1.0 的自动下载。此选项失去了自动更新 NuGet 依赖项的能力,但使应用程序的部署无需配置。

虽然正确的问题解决方案在于由包的所有者修复上述 NuGet 包的不一致,但有一种烦人的感觉。在源代码处修复后,不需要 System.Net.Http 包使用代码的解决方法。

问题:

  1. 为什么 System.Net.Http v 4.3.0 包包含不匹配的 System.Net.Http.dll v 4.1.1 而有更早的确切版本包 4.1.1?
  2. 我们是否应该继续使用上述 2 种解决方法中的任何一种?
  3. 哪个更好?
  4. 或者:这个问题还有别的解决办法吗?
  5. 或者:是否即将对 NuGet 包进行更新以解决不一致问题?

谢谢。

我觉得回答我自己的问题实际上是正确的,因为 99% 的答案已经存在。

github/corefx 的开发团队承认 this issue resolution would be a side effect of another known issue 修复了从 System.Net.Http 项目中删除对 System.Diagnostics.DiagnosticSource.dll 的硬引用的性质。

到那时:可以根据个人喜好使用所提供的两种解决方法中的任何一种。

我从 NuGet 安装 System.Net.Http(版本 4.3.1)解决了这个问题。

在我的例子中

FileLoadException: Could not load file or assembl y 'System.Diagnostics.DiagnosticSource, Version=4.0.3.0, Culture=neutral, Public KeyToken=cc7b13ffcd2ddd51'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

何时开始 Asp.Net 添加了 Core 2.0 项目 Microsoft.AspNetCore.TestHost 包“2.1.0-preview2-final”。 修复是安装稳定版本“2.0.2”

程序集绑定重定向和添加对 System.Net.Http 的引用都不适合我。我不知道为什么。最终对我有用的是删除版本规范并直接向 DLL 指定 HintPath。所以,我从这个开始:

<Reference Include="System.Diagnostics.DiagnosticSource, Version=6.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">

对此:

<Reference Include="System.Diagnostics.DiagnosticSource, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
  <HintPath>..\packages\System.Diagnostics.DiagnosticSource.6.0.0\lib\net461\System.Diagnostics.DiagnosticSource.dll</HintPath>
  <Private>True</Private>
</Reference>

这似乎成功了。我不喜欢失去直接版本参考,但似乎没有任何其他方式。