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

5ssjco0h  于 2023-08-08  发布在  .NET
关注(0)|答案(6)|浏览(133)

根据NuGet,我正在做的项目安装了System.Net.Http版本4.3.3。它在Web.config文件中有一个绑定重定向,将其重定向到www.example.com版本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


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

pengsaosao

pengsaosao1#

不要使用任何bindingRedirectSystem.Net.Http,从配置文件中删除,不要认为开关版本正在使用,这是由您的系统管理
我发现把它添加到配置文件会导致严重问题,因为每个文件使用不同版本的dll。

从配置中删除System.Net.Http解决了我的疯狂问题,导致错误和找不到程序集。

eanckbw9

eanckbw92#

事实证明,我的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无效。

jtoj6r0c

jtoj6r0c3#

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

kr98yfug

kr98yfug4#

是的。内核版本和dll的汇编版本是不同的。确保你清楚地理解其中的区别。
一个nuget包可能是4.1.1,里面的dll的汇编版本不需要是4.1.1或任何与4.1.1相关的版本。
我们在所有dll引用中指定dll版本。而且nuget引用是这样的,一旦项目开始构建,所需的dll就可以在包中使用,这样它就可以被带到本地bin中供编译器使用。

7uzetpgm

7uzetpgm5#

您还可以将newVersion更改为项目中当前存在的版本:
1.在VS内部的 * 搜索解决方案资源管理器 * 中搜索System.Net.Http字符串,并通过检查该dll的属性或通过检查属性窗口(f4)的引用来查找此程序集的版本
1.将newVersion更改为项目中现有的版本。例如4.4

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

字符串
有时像@Mahdi的答案一样删除整个依赖性有助于https://stackoverflow.com/a/63031440/184572

7gyucuyw

7gyucuyw6#

我认为他们在这个dll的版本之间改变了publicKeyToken,因为这不起作用:

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

字符串
但这个很有效

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

相关问题