System.Net.Http的绑定重定向不起作用 - 为什么?

时间:2018-03-05 18:21:42

标签: c# asp.net nuget

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

这是什么?重定向是如何工作的,以及为什么它现在不起作用?

5 个答案:

答案 0 :(得分:4)

事实证明,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没有。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

雅。 nuget版本和dll的汇编版本是不同的。确保你清楚地理解这些差异。

nuget包可能是4.1.1,其中的dll的汇编版本不需要是4.1.1或与4.1.1相关的任何内容。

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

答案 3 :(得分:0)

不要为anzahlint.toString() --> anzahlint.getEditText().getText().toString()使用任何bindingRedirect,从配置文件中删除,不要认为使用的是女巫版本,这是由您的系统管理的

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

从CONFIG中删除System.Net.Http可以解决我的疯狂问题,该问题导致错误并且找不到程序集。

答案 4 :(得分:0)

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

  1. Search solution Explorer 中搜索 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