10
votes

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
Because, basically, MS screwed up: github.com/dotnet/corefx/issues/11100Gaute Løken
Really? Didn't get an idea what happened, can you explain in answer?astef
They added some features to System.Net.Http for .net core. When targetting it (through .NET Standard) from a framework 4.6.1/4.6.2 app, there was a regression due to security requirement that was added, which broke things in framework apps. But they couldn't recover from it by adding a redirect to an older version, as that would potentially break apps using those new features. So it's left as an exercise for the developer to maintain redirects, which nuget will overwrite. Yep, it's a mess. :(Gaute Løken
The positive is that if you change your redirect to target 4.0.0.0, chances are it will work.Gaute Løken

2 Answers

2
votes

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.

1
votes

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.