Why can't I use the System.Net.Http package in the solution with the System.Net.Http link?

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

Then I created ConsoleApp (.NET Framework 4.6.2) referencing ClassLibrary and an instance of Class1 in the Main method.

Now running ConsoleApp raises an exception at runtime:

Unhandled exception: System.IO.FileNotFoundException: Failed to 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 specified file. in ClassLibrary1.Class1..ctor () in ConsoleApp1.Program.Main (String [] args) in [...]

In the detailed build log, I see this message:

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, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a" was selected because it was primary and "System.Net.Http, Version = 4.1.1.1, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a "was not.

Suppose I cannot remove the link to System.Net.Http from ConsoleApp , because I have this situation in the real structure of the project.

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

I searched for it, and this is a popular problem, but I did not find a clear explanation of what is actually happening, and how to fix such cases in general.

Failed to load file or assembly System.Net.Http version 4.1.1.0

Failed to load file or assembly "System.Net.Http" or one of its dependencies

About conflicts:

What does .NET mean when choosing "primary" when choosing between a conflict dll link?

Conflicts were found between different versions of the same dependent assembly that could not be fixed.

+5
source share
1 answer

I wonder why you decided to use the NuGet package instead of using the link manager to load into the corresponding assembly. By default, in many project templates, VS includes System.Net.Http. If so, you used NuGet explorer to install the build, then one of two options should help:

  • Uninstall the NuGet package and use the designated version from the link manager (see the "Assemblies / Framework" section), although I'm sure it is already selected.

  • In the link manager, uncheck the highlighted version of System.Net.Http and use the one you installed using NuGet.

Personally, I think that option 1 is better if you absolutely do not need something specific, found only in the latest version of NuGet.

+1
source

Source: https://habr.com/ru/post/1270237/


All Articles