1

I've a console application targeting .NET 4.6 with a dependency on the NuGet package System.Net.Http v2.0.20126.16343. Everything was working just fine until I decided to upgrade my Visual Studio installation to 15.7.1 (27703.2000) from 15.2 (26430.14) because of the notifications.

Post upgrade, my application fails to build and I get a missing reference on System.Net.Http even though the package exists. If I remove and add the package again, Visual Studio references the library from GAC and not the packages folder.

The only way I can get my application to build successfully if I upgrade the System.Net.Http package to v4.3.3 but I'll not be able to upgrade any NuGet packages now due to constraints beyond my control. I will also be not able to uninstall Visual Studio and go back to v15.2 again due to external constraints.

Is there any way that I can fix this and stay on v2.0.20126 of System.Net.Http while using VS 15.7.1?

Appreciate your help!

Balaji
  • 53
  • 1
  • 4

2 Answers2

1

Add an app.config and add assemblyBinding for the System.Net.Http:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="3750abcc3150b00c" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.3.3.0" newVersion="2.0.20126.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

(I haven't tried this, look if this works for your old version or not)

But this package is only for .net 4.0 and maeks no sense for .net 4.6. Here the last 4.3.3 has a .net 4.6 library which adds new features. So update the Nuget package to last version 4.3.3 would be the better solution.

magicandre1981
  • 27,895
  • 5
  • 86
  • 127
  • Appreciate your response. Will try tomorrow at work and get back to you. Also, is the 4.3.3 backward compatible with 2.0.20126? This is not my project and I am not really sure what they are using it for. Will upgrading to 4.3.3 break anything? – Balaji May 20 '18 at 22:24
  • the package only added new things to .net 4.0, so for your .net 4.6, you can delete the package or try the last 4.3.3. Try it out for what it was used. – magicandre1981 May 21 '18 at 07:09
1

Force application to use NuGet version of library instead of GAC

That because your console application targeting .NET 4.6. The nuget package is used for .NET 4.0 (<4.5), where System.Net.Http did not exist. When your app targets .NET Framework 4.5+, it will just forward to System.Net.Http which is part of the platform.

This is by design in apps running on .NET Framework 4.5+.

So, to resolve this issue you can switched your project to .NET 4.0 profile:

  1. Switched my project to .NET 4.0 profile.
  2. Uninstalled Web API NuGet package.
  3. Installed Web API (Beta) NuGet package again.
  4. Verified that .csproj file contains for ALL referenced assemblies, so it will always take it from Bin folder, instead of GAC.

Certified: Could not load file or assembly 'System.Net.Http, Version=2.0.0.0 in MVC4 Web API

If you do not want to switched your project to .NET 4.0, you can try the magicandre1981`s suggestion, update the that package to 4.1.0+, which including .net 4.6 library.

Besides, you can refer to this thread for some more details.

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • Hi Leo, thanks for the response. While I do understand that the package is not meant for 4.5, why did the old version of Visual Studio allow the solution targeting 4.6 to refer the package that was not meant for 4.6? Also @magicandre1981's solution did not seem to work. With regards to downgrading the project to 4, that is not possible as well as there are dependencies that were built on 4.6. – Balaji May 24 '18 at 02:52
  • @Balaji, Thanks for your reply. Indeed, this is a issue after update Visual Studio to 15.7, someone already reported this, but not got a exact reply. The workaround is update the package to 4.1.0+, is this not work for you? Or you just want to use the version v2.0.20126? – Leo Liu May 24 '18 at 03:06
  • 1
    @Balaji, Any update for this issue? The .NET Framework 4.5+ comes with System.Net.Http assembly ready to use, so there is no need to reference any nuget packages. You can check davidsh`s comment for details **If you're building a .NET Framework 4.6.x app, you should just use standard references to System.Net.Http and not bring in any Nuget package for System.Net.Http.** https://github.com/dotnet/corefx/issues/25773 – Leo Liu May 25 '18 at 07:40