简体   繁体   English

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

[英]Binding redirect for System.Net.Http doesn't work - why?

The project I'm working on has System.Net.Http version 4.3.3 installed, according to NuGet.根据 NuGet,我正在处理的项目安装了 System.Net.Http 版本 4.3.3。 It had a binding redirect in the Web.config file to redirect it to version 4.2.0.0, which worked for some reason:它在 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>

I changed it to point to 4.3.3.0 which is the version NuGet says is installed:我将其更改为指向 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>

... but now I get this error when I try to debug the ASP.NET site: ...但现在当我尝试调试 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.

To make things even more confusing, the DLL version in the bin directory for the site seems to be different again:更令人困惑的是,站点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

What's up with this?这是怎么回事? How did the redirect work before, and why doesn't it work now?重定向以前是如何工作的,为什么现在不工作了?

do not use any bindingRedirect for System.Net.Http , remove from config file and do not think witch version is in use, this is managed by your system不要为System.Net.Http使用任何bindingRedirect ,从配置文件中删除并且不要认为女巫版本正在使用中,这由您的系统管理

i found that adding it to config file cause serious problem, because each file use diffrent version of this dll.我发现将它添加到配置文件会导致严重问题,因为每个文件都使用此 dll 的不同版本。

REMOVING System.Net.Http FROM CONFIG resolve my insane problem that cause errors and not found assembly.从 CONFIG 中删除System.Net.Http解决了导致错误和找不到程序集的疯狂问题。

It turns out that the DLL version in my bin directory was actually version 4.2.0.0 , which is apparently bundled with Visual Studio 2017, and the version distributed in NuGet package version 4.3.3 is DLL version 4.1.1.2 , which wasn't being used at all.事实证明,我的bin目录中的 DLL 版本实际上是版本4.2.0.0 ,它显然与 Visual Studio 2017 捆绑在一起,而 NuGet 包版本4.3.3中分发的版本是 DLL 版本4.1.1.2 ,这不是完全用过。 Confusing!令人困惑! But this explains why the redirect to DLL version 4.2.0.0 worked and redirecting to 4.3.0.0 didn't.但这解释了为什么重定向到 DLL 版本4.2.0.0有效而重定向到4.3.0.0无效。

I had similar issue in past.我过去有类似的问题。 Most probably you have other assemblies in your.csproj which still have dependency on old version ie 4.2.0.0很可能你在 your.csproj 中还有其他程序集,它们仍然依赖于旧版本,即 4.2.0.0

Ya exactly.没错。 The nuget versions and the dll's assembly versions are different. nuget 版本和 dll 的汇编版本不同。 Make sure you understand the difference clearly.确保您清楚地了解其中的区别。

A nuget package maybe 4.1.1 and the assembly version of the dlls inside it needn't be 4.1.1 or anything related to 4.1.1.一个 nuget 包可能是 4.1.1,它里面的 dll 的程序集版本不需要是 4.1.1 或与 4.1.1 相关的任何东西。

We specify dll versions in all the dll references.我们在所有 dll 引用中指定 dll 版本。 And nuget references are made so that once the project is building, the required dlls are made available in the packages, so that it can be taken and moved to the local bin for the compiler to use.并且进行了 nuget 引用,以便在构建项目后,所需的 dll 在包中可用,以便可以将其移动到本地 bin 以供编译器使用。

You can also change the newVersion to the version that exists currently in your project:您还可以将 newVersion 更改为项目中当前存在的版本:

  1. search for System.Net.Http string in Search solution Explorer inside VS and find the version of this assembly by checking property of that dll or reference by checking property window(f4)在 VS 中的搜索解决方案资源管理器中搜索 System.Net.Http 字符串,并通过检查该 dll 的属性或通过检查属性窗口(f4)的引用来查找此程序集的版本

  2. change the newVersion to the one that exists in your project.newVersion更改为项目中存在的版本。 for example 4.4例如 4.4

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

sometimes removing the whole depencency like the answer by @Mahdi helps https://stackoverflow.com/a/63031440/184572有时像@Mahdi 的回答那样删除整个依赖关系有助于https://stackoverflow.com/a/63031440/184572

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM