【问题标题】:Binding redirect for System.Net.Http doesn't work - why?System.Net.Http 的绑定重定向不起作用 - 为什么?
【发布时间】:2018-03-05 18:21:41
【问题描述】:

根据 NuGet,我正在处理的项目安装了 System.Net.Http 版本 4.3.3。它在 Web.config 文件中有一个绑定重定向,可将其重定向到 4.2.0.0 版本,但出于某种原因,该重定向有效:

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

我将其更改为指向 4.3.3.0,即 NuGet 所说的已安装版本:

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

...但是现在当我尝试调试 ASP.NET 站点时出现此错误:

Could not load file or assembly 'System.Net.Http' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

=== Pre-bind state information ===
LOG: DisplayName = System.Net.Http
 (Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: System.Net.Http | Domain ID: 2
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/.../Dev/Src/Web/PortalSite/
LOG: Initial PrivatePath = C:\...\Dev\Src\Web\PortalSite\bin
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\...\Dev\Src\Web\PortalSite\web.config
LOG: Using host configuration file: C:\...\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/.../AppData/Local/Temp/Temporary ASP.NET Files/vs/e31848dc/3694ba06/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/.../AppData/Local/Temp/Temporary ASP.NET Files/vs/e31848dc/3694ba06/System.Net.Http/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/.../Dev/Src/Web/PortalSite/bin/System.Net.Http.DLL.
LOG: Using application configuration file: C:\...\Dev\Src\Web\PortalSite\web.config
LOG: Using host configuration file: C:\...\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Redirect found in application configuration file: 4.2.0.0 redirected to 4.3.3.0.
LOG: Post-policy reference: System.Net.Http, Version=4.3.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: Attempting download of new URL file:///C:/.../AppData/Local/Temp/Temporary ASP.NET Files/vs/e31848dc/3694ba06/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/.../AppData/Local/Temp/Temporary ASP.NET Files/vs/e31848dc/3694ba06/System.Net.Http/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/.../Dev/Src/Web/PortalSite/bin/System.Net.Http.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Minor Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

为了让事情更加混乱,该站点的bin 目录中的 DLL 版本似乎又有所不同:

PS C:\...\Dev\Src\Web\PortalSite\bin> (Get-Item ...\bin\Debug\System.Net.Http.dll).VersionInfo

ProductVersion   FileVersion      FileName
--------------   -----------      --------
4.6.25908.02 ... 4.6.25908.02     C:\...\Dev\Src\Web\PortalSite\bin\System.Net.Http.dll

这是怎么回事?重定向以前是如何工作的,为什么现在不能工作?

【问题讨论】:

  • 不要对 System.Net.Http 使用任何 bindingRedirect ,从配置文件中删除并且不要认为正在使用女巫版本,这是由您的系统管理的

标签: c# asp.net nuget


【解决方案1】:

原来我bin目录下的DLL版本其实是4.2.0.0版本,显然是与Visual Studio 2017捆绑的,而NuGet包版本4.3.3中分发的版本是DLL版本4.1.1.2,根本没有被使用。令人困惑!但这解释了为什么重定向到 DLL 版本 4.2.0.0 有效而重定向到 4.3.0.0 无效。

【讨论】:

【解决方案2】:

不要为System.Net.Http 使用任何bindingRedirect,从配置文件中删除并且不要认为正在使用女巫版本,这是由您的系统管理的

我发现将它添加到配置文件会导致严重的问题,因为每个文件都使用不同版本的这个 dll。

REMOVING System.Net.Http FROM CONFIG 解决了导致错误和未找到程序集的疯狂问题。

【讨论】:

  • 非常感谢,在将 web 项目从 4.7.1 升级到 4.7.2 后,我还升级了测试项目。它仍然有一个用于 System.Net.Http 的 bindingRedirect,并且在加载 Web 程序集以伪造 ResourceManager 时,它因另一个错误而崩溃,我不应该加载引用程序集......但是在删除绑定重定向后,我的测试项目可以工作很好!!!
【解决方案3】:

我过去也遇到过类似的问题。很可能您的 .csproj 中有其他程序集仍然依赖于旧版本,即 4.2.0.0

【讨论】:

  • 但是为什么绑定不重定向到 4.3.3.0?
  • 你的 o/p 目录下的 dll 是什么版本?
  • 如果你的 o/p 目录有 4.2 那么你怎么能把它重定向到 4.3 呢?重定向只说 - “亲爱的 CLR 在运行时如果你被要求加载 4.3,请加载 4.2,因为这就是我让你可用的”
  • 什么是o/p目录?
  • 确定问题的最佳方法是我建议使用进程资源管理器并查看该 dll 的加载位置。我这么说是因为我们在从 GAC 加载 dll 时遇到了类似的问题
【解决方案4】:

没错。 nuget 版本和 dll 的程序集版本不同。确保您清楚地了解其中的区别。

一个 nuget 包可能是 4.1.1,其中的 dll 的程序集版本不必是 4.1.1 或任何与 4.1.1 相关的东西。

我们在所有 dll 引用中指定 dll 版本。并且进行了 nuget 引用,以便在构建项目后,所需的 dll 在包中可用,以便可以将其获取并移动到本地 bin 以供编译器使用。

【讨论】:

    【解决方案5】:

    您还可以将 newVersion 更改为项目中当前存在的版本:

    1. 在 VS 内的搜索解决方案资源管理器中搜索 System.Net.Http 字符串,并通过检查该 dll 的属性或通过检查 属性窗口(f4)

    2. newVersion 更改为项目中存在的版本。例如 4.4

       <bindingRedirect oldVersion="0.0.0.0-4.3.3.0"  newVersion="4.4"  />
      

    有时像@Mahdi 的回答那样消除整个依赖有助于https://stackoverflow.com/a/63031440/184572

    【讨论】:

      猜你喜欢
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 2010-10-07
      • 2021-10-31
      • 2011-08-21
      • 1970-01-01
      • 2017-04-02
      相关资源
      最近更新 更多