无法加载文件或程序集'System.Net.Http'

34

在我的构建输出中,我的诊断显示了这个冲突:

“System.Net.Http,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”和“System.Net.Http,Version=4.1.1.2,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”之间存在冲突。(TaskId:20) “System.Net.Http,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”被选择是因为它是主要的,“System.Net.Http,Version=4.1.1.2,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”没有被选择。(TaskId:20) 引用依赖于“System.Net.Http,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”的内容[C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.6.1\System.Net.Http.dll]。(TaskId:20)

我添加了RestSharp nuget包才出现了这个问题。在安装它之前,我没有遇到任何问题,我认为其中的一个部分可能导致了这个问题。我试图卸载它,但那并没有解决问题。


1
你是否设置了正确的绑定重定向到版本4.1.1.2?如果绑定重定向设置不正确,可能会使用4.0.0.0作为主要程序集。 - Tetsuya Yamamoto
1
我找不到引用4.0.0的绑定。 <dependentAssembly> <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.1.1.2" newVersion="4.1.1.2" /> </dependentAssembly> - Dan
13
解决方案:我从配置文件中删除了绑定。现在一切都正常工作。因此你可以尝试这个方法。 - Dan
6
@Dan谢谢你在评论中提供了你的解决方案,它帮助我在完全相同的情况下解决了问题。也许你可以将这个解决方案作为答案并标记为未来其他人参考的答案!:) 谢谢! - DeeKayy90
1
请不要在配置文件中使用任何bindingRedirect来处理System.Net.Http的版本问题。您无需考虑正在使用哪个版本,这由您的系统管理。 - Mahdi
7个回答

20

安装 Nuget 包: System.Net.Http 版本 4.3.3 安装了正确的 Version=4.1.1.2

这将导致您的项目文件中出现以下引用:

<Reference Include="System.Net.Http, Version=4.1.1.2, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>..\..\..\..\packages\System.Net.Http.4.3.3\lib\net46\System.Net.Http.dll</HintPath>
</Reference>

2
我将此标记为答案。虽然对我来说删除引用起作用了,但可能掩盖了其他问题。请尝试两种方法,并选择最适合您的那个。 - Dan
@Dan,你最终做了什么长期计划? - William T. Mallard
我实际上不得不将 System.Net.Http.dll 复制到 ../bin/Debug 文件夹中才能使其正常工作,不知何故安装 NuGet 包并没有将该 dll 放在那里。另请参见 https://gertjanvanmontfoort.blogspot.com/2017/11/systemnethttp-dll-version-problems.html。 - Mike Wise
1
我使用以下解决方案修复了这个问题: <configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly> <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.0.0.0" /> </dependentAssembly> </assemblyBinding>
</runtime>
</configuration>
- Saeed Ahmad
1
确实,@saeed-ahmad的解决方案可行! - Andrii

15
如果您使用的是vs2017,一些项目会强制引用system.net.http(4.2.0.0)。当您从nuget安装(版本4.3.3)时,您的system.net.http将是4.1.1.2。这将导致冲突。因此,在您的web.config或app.config中,您可以根据运行时复制到bin文件夹中的版本更改为4.1.1.2或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.1.1.2" />
      </dependentAssembly>

对我来说基本上解决了这个问题 - 对我有效的实际配置(Azure App Service):https://github.com/domaindrivendev/Swashbuckle/issues/784#issuecomment-437003960 - Jay Cummins
注意:您的生产环境(或部署代码的服务器)通常没有安装VS2017,因此其版本将是4.1.1.2,而您的本地开发环境(带有VS安装)将是4.2.0.0。 - Wolf
Sendgrid库依赖于System.Net.Http。我不得不将.NET Framework从4.6.1升级到4.7.1。 - Kishan Vaishnav

14

将4.6.1框架升级到4.7.2后,我们开始收到此错误提示。最终解决方法是进入web.config文件,找到以下内容:

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

并将其替换为

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

上述更改只是这样的:newVersion="4.0.0.0"


12
我正在使用Visual Studio 15.5.NET Framework 4.7.2。当我将System.Net.Http 4.3.3升级到4.3.4时,我收到了“无法加载”的错误消息:

{"Could not load file or assembly 'System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.":"System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"}

我终于解决了这个问题,但是又遇到了FileNotFoundException

System.IO.FileNotFoundException HResult=0x80070002 Message=Could not load file or assembly 'System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

.NET Core项目在GitHub上讨论了这个问题,我发现.NET Core项目中的这篇文章非常有帮助:

https://github.com/dotnet/corefx/issues/25773#issuecomment-350036434

我的(简陋)总结:微软试图消除对System.Net.Http NuGet包的需求,因为它是其他版本问题的解决方法。 .NET Framework 4.7.1VS 15.5进行了更改,应该会彻底消除对其及相关绑定的需求。

我从我的项目中删除了System.Net.Http NuGet包,并添加了对.NET Framework 4.7.2中包含的System.Net.Http的引用,现在一切都顺利了。


我在使用VS 2019和Net Framework 4.8时,使用System.Net.Http NuGet包时出现了错误。使用绑定重定向无效。 - Kiquenet
1
好的。请尝试按照我的建议移除NuGet包和绑定。我的VS2019/4.8项目在没有它们的情况下可以正常工作。 - skst

11

根本原因:当您在应用程序中引用某些第三方库时,通常会出现这种问题。

例如,您从NuGet引用了 RestSharp (第三方库)。该RestSharp可能已经使用了 System.Net.Http 4.2.0.0 版本的引用。而您的项目也引用了 System.Net.Http 4.0.0.0 (来自GAC)的引用。现在当您运行应用程序并尝试调用任何使用 RestSharp 的方法时,同时运行时(CLR)尝试定位 System.Net.Http 版本为 4.2.0.0 的程序集,当它无法找到所需的版本时,它会抛出 System.IO.FileNotFoundException 异常,并显示以下错误消息。

  

无法加载文件或程序集“System.Net.Http,Version = 4.2.0.0,   Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a”或其   其中一项依赖关系。系统找不到指定的文件。

您可以在此处看到如何运行时查找程序集。

解决方案: 只需在启动项目的 web.config app.config 中添加以下配置。

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

这个配置指示运行时只使用 4.0.0.0 版本的System.Net.Http程序集来解析任何版本从0.0.0.04.2.0.0的该程序集。

以下是完整的架构:

<configuration>  
   <runtime>  
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
        <dependentAssembly>
            <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.0.0.0" />
        </dependentAssembly>
      </assemblyBinding>  
   </runtime>  
</configuration> 

2
系统.net.http库现在作为.Net标准框架的一部分进行分发,这可能会导致问题,通常是在更新Nuget包到较新版本时出现。对我有效的解决方案是从Nuget中删除System.Net.Http包(以及依赖于它的格式化程序包),并删除app.config中对system.net.http库的所有引用(它们经常是dependentAssemblies)。最后,编辑您的项目文件以添加对来自框架的http的引用,如下所示:

enter image description here


0

如果您不想使用bindingRedirect,这里有一个不太好但可替代的解决方案 - 如果您有-

<startup useLegacyV2RuntimeActivationPolicy="true" >
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" /></startup>

在 App.config 中,删除 "useLegacyV2RuntimeActivationPolicy" 属性可以帮助避免回退到旧的 CLR。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接