本文介绍了为什么我不能在具有System.Net.Http参考的解决方案中使用System.Net.Http包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用VS2017,我在一个空解决方案中创建了一个 ClassLibrary (.NET Framework 4.6.2)。然后,我在其中安装了软件包,在 Class1 构造函数中使用 HttpClient 类。

Using VS2017, I've created a ClassLibrary (.NET Framework 4.6.2) in an empty solution. Then I've installed System.Net.Http 4.3.2 package there and used HttpClient class in a Class1 constructor.

然后我创建了一个 ConsoleApp (.NET Framework 4.6.2),引用了 ClassLibrary ,并在< Class1 中实例化了 Main 方法。

Then I've created a ConsoleApp (.NET Framework 4.6.2), referenced ClassLibrary and instantiated Class1 in the Main method.

现在运行 ConsoleApp 会导致运行时异常:

Now running ConsoleApp causes runtime exception:

在详细的构建日志中,我看到以下消息:

In detailed build log I see this message:

2>选择 System.Net.Http,版本= 4.0.0.0,文化=中性,
PublicKeyToken = b03f5f7f11d50a3a和
System.Net.Http,版本= 4.1.1.1,文化=中性,
PublicKeyToken = b03f5f7f11d50a3a不是。

2> "System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" was chosen because it was primary and "System.Net.Http, Version=4.1.1.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" was not.

假设我无法从 ConsoleApp 中删除对 System.Net.Http 的引用,因为我确实遇到了这种情况项目结构。

Let's suppose I can not remove reference to System.Net.Http from ConsoleApp, because I have this situation in a real project structure.


  • 使用特定版本参数播放 System.Net.Http
    引用没有帮助。

  • 指定< bindingRedirect oldVersion = 0.0.0.0-4.1.1.1
    newVersion = 4.1.1.1 />
    没有帮助

  • Playing with Specific Version parameter of System.Net.Httpreference did not help.
  • Specifying <bindingRedirect oldVersion="0.0.0.0-4.1.1.1"newVersion="4.1.1.1" /> did not help

我已经用它搜索了一下,这是一个很普遍的问题,但是我没有找到关于实际发生的情况以及如何解决此类问题的明确解释。

I've googled it and it is a popular problem, but I didn't find a clear explanation of what actually happens and how to fix such cases in general.

关于冲突:

推荐答案

我想知道为什么您选择使用NuGet包而不是使用引用管理器来加载适当的程序集。默认情况下,许多项目模板中VS都包含System.Net.Http。如果是使用NuGet软件包资源管理器来安装程序集,则这两个选项之一应该会有所帮助:

I wonder why you are choosing to use the NuGet package instead of using the Reference Manager to load in the appropriate assembly. By default in a lot of project templates VS includes System.Net.Http. If it is the case that you used the NuGet package explorer to install the assembly, then one of the two options should help out:


  1. 删除NuGet软件包,并从参考管理器中使用分配的版本(在组件/框架下查看),尽管我敢打赌已经选择了该版本。

  1. Remove the NuGet package and use the assigned version from the Reference Manager (look under Assemblies / Framework) though I am betting this is already selected.

在参考管理器取消选择System.Net.Http的分配版本,并使用与NuGet一起安装的版本。

In the Reference Manager un-select the assigned version of System.Net.Http and use the one that you installed with NuGet.

我个人认为选项1更好,除非您绝对需要仅在NuGet最新版本中找到的特定内容。

Personally I think option 1 is better unless you absolutely need something specific found only in the NuGet latest version.

这篇关于为什么我不能在具有System.Net.Http参考的解决方案中使用System.Net.Http包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 03:08