Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 447
astef Avatar asked Jul 26 '17 16:07

astef


Video Answer


1 Answers

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. 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.

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

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

like image 125
Kodaloid Avatar answered Sep 30 '22 01:09

Kodaloid