astef
astef

Reputation: 9488

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.

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

Upvotes: 11

Views: 8911

Answers (2)

Jacob Gaiski
Jacob Gaiski

Reputation: 576

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.

Upvotes: 1

Kodaloid
Kodaloid

Reputation: 1326

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.

Upvotes: 2

Related Questions