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

Strange issue with System.Net.Http 4.2.0.0 not found(未找到 System.Net.Http 4.2.0.0 的奇怪问题)

本文介绍了未找到 System.Net.Http 4.2.0.0 的奇怪问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题,让我发疯……

I have a strange issue, which drives me crazy…

我有一个简单的类库项目(完整的 .NET Framework,4.6.1),其中包含一个用于 Cosmos DB 功能的包装类.因此,我将Microsoft.Azure.DocumentDB"NuGet Package 1.19.1 添加到该项目中.除此之外,我还参考了Newtonsoft.Json"NuGet Package 10.0.3,以及几个Microsoft.Diagnostics.EventFlow.*".NuGet 包.

I have a simple Class Library Project (Full .NET Framework, 4.6.1) with a wrapper class for functionality around Cosmos DB. Therefore I have added the "Microsoft.Azure.DocumentDB" NuGet Package 1.19.1 to this project. Other than that, I have a reference to the "Newtonsoft.Json" NuGet Package 10.0.3, as well as to a couple of "Microsoft.Diagnostics.EventFlow.*" NuGet Packages.

到目前为止,一切编译都没有任何错误.

So far, everything compiles without any error.

但只要我点击我的包装类 - 从一个简单的 Service Fabric 无状态服务(完整的 .NET Framework 4.6.1)消耗 - 并尝试执行以下代码行:

But as soon as I hit my wrapper class – consumed from a simple Service Fabric Stateless Service (Full .NET Framework 4.6.1) – and try to execute the following line of code:

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

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

I get this strange error at runtime:

System.IO.FileNotFoundException 发生 HResult=0x80070002
消息=无法加载文件或程序集 'System.Net.Http,版本=4.2.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a' 或它的依赖项之一.系统找不到指定的文件.
来源= StackTrace:在Microsoft.Azure.Documents.Client.DocumentClient.Initialize(UriserviceEndpoint, ConnectionPolicy connectionPolicy, Nullable1 desiredConsistencyLevel) 在 Microsoft.Azure.Documents.Client.DocumentClient..ctor(Uri serviceEndpoint, String authKeyOrResourceToken, ConnectionPolicy connectionPolicy, Nullable1 desiredConsistencyLevel)

System.IO.FileNotFoundException occurred 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.
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)

内部异常 1:FileNotFoundException:无法加载文件或程序集'System.Net.Http,版本=4.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a' 或其依赖项之一.这系统找不到指定的文件.

Inner Exception 1: FileNotFoundException: Could not load file or assembly 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

我完全不知道为什么根本找不到 System.Net.Http 程序集——在我的类库项目中甚至有一个对 .Net 框架程序集System.Net.Http 4.0.0.0"的程序集引用.

I have absolutely no clue, why the System.Net.Http assembly is not found at all – there is even a assembly reference in my class library project to the .Net Framework Assembly "System.Net.Http 4.0.0.0".

我也不明白的是,有一个奇怪的绑定重定向到 4.2.0.0——那个是从哪里来的?为了解决这个问题,我尝试将以下重定向添加到 Service Fabric 服务的 app.config(它正在使用类库):

What I also do not understand is, that there is this weird binding redirect to 4.2.0.0 – where is that one coming from? To get around this one, I tried to add the following redirect to the app.config of the Service Fabric Service (which is consuming the class library):

但仍然没有区别,我仍然在运行时收到错误.

But still no difference, I still get the error at runtime.

有人知道吗?有人见过这样的问题吗?

Anybody having a clue? Anybody having seen such issue?

推荐答案

您遇到的问题与 Visual Studio 有关,尤其是 System.Net.Http v4.2.0.0 附带的 2017.然而,采用新的方式,任何引用都应该通过 NuGet 完成,最新版本的 System.Net.Http 是 4.3.3 包含 dll 版本 4.1.1.2.

The problem you're facing is related to Visual Studio, especially 2017 which is shipped with System.Net.Http v4.2.0.0. However, adopting the new way whereby any references should be done via NuGet, latest version of System.Net.Http which is 4.3.3 contains the dll version 4.1.1.2.

问题是 VS 在构建时和运行时也会忽略您的引用,它会尝试引用它知道的 DLL.

The problem is that VS at build time and at run time as well will ignore your reference and it will try to reference the DLL it knows about.

如何解决:

  • 确保对 System.Net.Http 的任何引用都是通过 NuGet 完成的
  • 构建时间错误: 更改随 VS 2017 (c:Program Files (x86)Microsoft Visual Studio2017ProfessionalMSBuildMicrosoftMicrosoft.NET.Build.Extensions et461lib);如果您有不同的版本,那么路径会略有不同,但差别不大
  • 运行时错误:添加程序集绑定重定向

  • make sure that any references to System.Net.Http are done via NuGet
  • Build time errors: change extension of System.Net.Http.dll (or move it somewhere else...basically get rid of it) that is shipped with VS 2017 (c:Program Files (x86)Microsoft Visual Studio2017ProfessionalMSBuildMicrosoftMicrosoft.NET.Build.Extensions et461lib); if you've got a different version then the path will slightly differ, not much though
  • Runtime errors: add an assembly binding redirect

如果您在 google 上在线查看,您会发现一些与 Microsoft 相关的未解决问题,因此希望他们将来会解决此问题.

If you look online on google you'll find a few open issues with Microsoft about this, so hopefully they'll fix this in the future.

希望这会有所帮助.

更新:

在寻找一些永久性修复此问题以在构建代理上运行时,注意到如果您迁移到新的 NuGet PackageReference 模型(在 .csproj 中而不是在 packages.config) 往往效果更好.这是有关如何进行此升级的指南的链接:https://docs.microsoft.com/en-us/nuget/reference/migrate-packages-config-to-package-reference

When looking at finding some permanent fixes for this issue to work on the build agents, noticed that if you migrate to the new NuGet PackageReference model (in .csproj not in packages.config) tends to work better. Here's a link to the guide on how to do this upgrade: https://docs.microsoft.com/en-us/nuget/reference/migrate-packages-config-to-package-reference

这篇关于未找到 System.Net.Http 4.2.0.0 的奇怪问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:未找到 System.Net.Http 4.2.0.0 的奇怪问题