使用 VS2017,我在一个空的解决方案中创建了一个 ClassLibrary (.NET Framework 4.6.2).然后我在那里安装了 System.Net.Http 4.3.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
主要
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:
未处理的异常:System.IO.FileNotFoundException:无法加载文件或程序集'System.Net.Http,版本=4.1.1.1,文化=中性,PublicKeyToken=b03f5f7f11d50a3a' 或其依赖项之一.这系统找不到指定的文件.在ConsoleApp1.Program.Main(String[] 处的 ClassLibrary1.Class1..ctor()args) 在 [...]
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Net.Http, Version=4.1.1.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. at ClassLibrary1.Class1..ctor() at ConsoleApp1.Program.Main(String[] args) in [...]
在详细的构建日志中,我看到了这条消息:
In detailed build log I see this message:
2>System.Net.Http, Version=4.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a" 和System.Net.Http,版本=4.1.1.1,文化=中性,PublicKeyToken=b03f5f7f11d50a3a".
2> There was a conflict between "System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" and "System.Net.Http, Version=4.1.1.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
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
的Specific Version
参数参考没有帮助. 没有帮助
Specific Version
parameter of System.Net.Http
reference did not help.<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.
可以不加载文件或程序集 System.Net.Http 版本 4.1.1.0
无法加载文件或程序集System.Net.Http"或其依赖项之一
关于冲突:
.NET 是什么意思在冲突 dll 引用之间进行选择时按主要"?
发现不同版本之间存在冲突无法解析的相同依赖程序集的错误
我想知道您为什么选择使用 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:
删除 NuGet 包并使用参考管理器中分配的版本(在程序集/框架下查看)虽然我打赌这已经被选中.
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 包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!