System.Net.Http 4.2.0.0未找到的奇怪问题

167

我有一个奇怪的问题,让我感到很疯狂...

我有一个简单的类库项目(完整的.NET框架,4.6.1),其中包含围绕Cosmos DB的功能的包装器类。因此,我将“Microsoft.Azure.DocumentDB” NuGet包1.19.1添加到了这个项目中。除此之外,我还引用了“Newtonsoft.Json” NuGet包10.0.3,以及一些“Microsoft.Diagnostics.EventFlow.*” NuGet包。

到目前为止,一切都可以编译而没有任何错误。

但是,一旦我使用一个简单的Service Fabric无状态服务(完整的.NET Framework 4.6.1)调用我的包装器类,并尝试执行以下代码行:

_docClient = new DocumentClient(new Uri(cosmosDbEndpointUrl), cosmosDbAuthKey);

我在运行时遇到了这个奇怪的错误:

System.IO.FileNotFoundException 异常 HResult=0x80070002
Message=无法加载文件或程序集“System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”或它的某一个依赖项。系统 找不到指定的文件。
Source= StackTrace: at Microsoft.Azure.Documents.Client.DocumentClient.Initialize(Uri serviceEndpoint, ConnectionPolicy connectionPolicy, Nullable1 desiredConsistencyLevel) at Microsoft.Azure.Documents.Client.DocumentClient..ctor(Uri serviceEndpoint, String authKeyOrResourceToken, ConnectionPolicy connectionPolicy, Nullable1 desiredConsistencyLevel)

Inner Exception 1: FileNotFoundException: 无法加载文件或程序集“System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”或它的某一个 依赖项。系统找不到指定的文件。

我完全不知道为什么根本找不到 System.Net.Http 程序集——我的类库项目甚至有对 .Net Framework 程序集 “System.Net.Http 4.0.0.0”的引用。

我也不理解为什么会有这个奇怪的绑定重定向到 4.2.0.0——这是从哪里来的呢?

为了解决这个问题,我尝试在 Service Fabric 服务的 app.config 中添加以下重定向:

但是仍然没有任何改变,我仍然在运行时遇到了这个错误。

有人知道原因吗?有人碰到过类似的问题吗?


我回答了这个问题:https://dev59.com/Fanka4cB1Zd3GeqPP45o#63031440 - Mahdi
如果前 5 个答案不适用于您,请参见我的答案,了解如何将程序集添加到 web.config 的编译部分。 - CrnaStena
使用VS时,有很多地方需要注意,比如GAC、Temp文件夹、packages文件夹以及隐藏的.AppData文件夹等等。希望微软能像node一样将VS的包都放在node_modules文件夹中,这样所有的文件都可以集中管理。遇到问题时,只需删除该文件夹,运行npm install命令,搞定 - Jeb50
26个回答

1

我找到了一个简单的解决方案,将Web项目的目标框架降级为4.6。

我创建了客户端和Web应用程序,客户端使用的是.NET Framework 4.7.1,Web应用程序也是如此,我遇到了相同的问题,当我将Web的目标.NET框架降级时,它就可以正常工作了。


1
我的解决方案与@Raquib的类似。我的项目是4.7.2,在我的电脑上运行得很好。每当我部署到我们的开发服务器时,我都会遇到问题。结果发现服务器上最高的.NET版本是4.6.1。将我的项目降级到4.6.1可以解决问题。升级服务器的.NET版本对我来说不是一个选项。

1

我尝试了各种解决方案(删除dependentAssembly或者同时指定binding redirect)。但是它们都没有起作用。

然而,对我有效的唯一解决方案是从Visual Studio中明确将System.Net.Http(或任何给你版本问题的DLL)的Specific Version设置为False。

enter image description here


这对我在一个多目标不同框架的项目中起作用,例如net472、net72等。 - Paul Hatcher

1
我遇到了类似的问题。试图删除 System.Net.Http NuGet 包(版本为 4.1.1.2)失败,因为它被标记为我正在使用的其他包的先决条件。
我尝试删除所有绑定重定向,就像几个答案中提到的那样。虽然这本身不起作用,但我继续进行了以下操作。
在所有项目的 References 节点下,我从 NuGet 包中删除了对 System.Net.Http.dll 的链接,并添加了来自 C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.6.1\System.Net.Http.dll 的链接。
所有这些引用都更改后,没有绑定重定向,项目构建并运行。

0

我曾经遇到过同样的问题。最终,我通过将Deploy-FabricApplication.ps1更改为以下内容来解决它

$binFolder = "$LocalFolder\..\..\..\..\Bin"

$httpDllLocation = "$binFolder\System.Net.Http.dll"
$codeFolder = "$ApplicationPackagePath\[ProjectName].ServicesPkg\Code"
$configFile = "$codeFolder\[ProjectName].Services.exe.config"

Copy-Item $httpDllLocation -Destination $codeFolder 

$appConfig = [xml](cat $configFile)
$appConfig.configuration.runtime.assemblyBinding.dependentAssembly | foreach {

    $name = $_.assemblyIdentity.name
    #Write $name
    if($name -eq 'System.Net.Http')
    {
        Write 'System.Net.Http changed'

        $_.bindingRedirect.newVersion = '4.2.0.0'
    }
}
$appConfig.Save($configFile)

我找到了一个版本为4.2.0.0的System.Net.Http.dll,它是x64位的。在部署此脚本之前,将该dll复制到包目录并更改配置文件以明确使用此文件。我还写了一篇关于我的System.Net.Http问题的博客,链接为http://gertjanvanmontfoort.blogspot.nl/2017/11/systemnethttp-dll-version-problems.html

不要忘记用您自己的项目名称替换[ProjectName]

希望这可以帮助您,如有任何问题,请随时提问。


0
我的解决方法是将 System.Net.Http 降级到 4.0 版本,这样所有的包和依赖项都将使用相同的版本:
  <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>

我已经从以下所有子文件夹中删除了System.Net.Http.dll:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\Microsoft.NET.Build.ExtensionsC:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework


这个答案看起来和我的答案很相似,但是位置有些不同。 - SandRock

0
在我的情况下,我删除了.config文件中<runtime>标签下的所有依赖项及其绑定重定向。我构建了解决方案,但出现了绑定错误。点击错误后,VS仅填充所需的依赖项及其重定向,并能够运行解决方案。

0
我使用的是 .net 4,并且我的 'System.Net.Http' 引用指向了 '.net 4.7' 版本的 dll。所以我将 .dll 路径更改为指向 'Microsoft.Net.Http' 包并重新构建它。这解决了我的问题。

enter image description here


0

在多台开发机上编译和运行该解决方案都没有问题,但有一台机器在运行项目时出现了以下错误。删除所有文件并从 TFS 检索它们并没有帮助。

通过 Visual Studio 修复解决了这个问题。

从 Visual Studio 安装程序中进行修复操作。

Visual Studio 2019 版本 16.11.5


0
在我的情况下,我只是从 app.config 中删除了一些汇编引用,然后它就可以工作了。

<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Text.Json" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.6" newVersion="6.0.0.6" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.0.1.2" newVersion="4.0.1.2" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Microsoft.IdentityModel.Clients.ActiveDirectory" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-5.2.9.0" newVersion="5.2.9.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.1.1.3" newVersion="4.1.1.3" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Microsoft.IdentityModel.Abstractions" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-6.24.0.0" newVersion="6.24.0.0" /> </dependentAssembly> </assemblyBinding> </runtime>


你应该提到是哪个包引起了问题。你真的需要删除整个列表吗? - HamsterWithPitchfork
FYI,我刚刚删除了System.Net.Http,这确实解决了我的问题。 - XIU

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