Why I can not use System.Net.Http package in a solution with System.Net.Http reference?

Viewed 8023

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.

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

Now running ConsoleApp causes runtime exception:

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

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

  • Playing with Specific Version parameter of System.Net.Http reference 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.

Could not load file or assembly System.Net.Http version 4.1.1.0

Could not load file or assembly 'System.Net.Http' or one of its dependencies

About conflicts:

What does .NET mean by 'primary' when choosing between conflict dll reference?

Found conflicts between different versions of the same dependent assembly that could not be resolved error

2 Answers

The best and easiest way to fix this issue, is with a binding redirect, as said. But the versioning looks off.

Simply specify the oldVersion as 0.0.0.0-5.0.0.0, and newVersion as 4.1.1.0

Where 4.1.1.0 is your version, for example.

Related