Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use latest version of System.Net.Http in .Net Framework

Tags:

c#

.net

nuget

The latest version of System.Net.Http on nuget is 4.3.4. But even the latest .Net Framework 4.8 ships with 4.2.0 of this library.

Even if I add the nuget package Visual Studio 2019 still picks up the System.Net.Http.dll from the .Net Framework installation files.

Is there any way to get around this?

.Net Core still does not have designer support for wpf and winforms that's why i need this to work with .net framework.

like image 875
imlokesh Avatar asked Feb 04 '23 17:02

imlokesh


2 Answers

Totally agree with you that this is confusing, but at the end binding redirection is your friend here used with your app.config / web.config file.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

The above snippet forces whatever version comes in with your code that the version which is specified in "newVersion" attribute will be used.

The table below gives you some insights on the versioning differences.

Mapping NuGet - BindingRedirect

Some info on the binding redirection by Microsoft itself.

like image 159
Dimi Takis Avatar answered Feb 06 '23 05:02

Dimi Takis


I have several projects where I was forced to install .NETStandard 2.0 because some other package was dependent on it, even though we're just using .NET 4.6.1. After a long time Googling (and sorry, I can't point you to where I found this because it was done so long ago), I was able to figure out that when you have packages that are using the netstandard2 version of the assemblies, VS will automatically force System.Net.Http to version 4.2.0, even if you don't have the binding redirect as explained by Dimi. Packages that install themselves with netstandard2 include System.Buffers, System.Collections.Immutable, System.Memory, System.Runtime.Compilers.Unsafe, and System.Text.Encodings.Web (I'm sure there are more).

What I ended up having to do was manually edit the .csproj files and force these assemblies to use the netstandard1.x versions.

<Reference Include="System.Memory, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
  <HintPath>..\..\packages\System.Memory.4.5.1\lib\netstandard2.0\System.Memory.dll</HintPath>
</Reference>

So for example, the hint path for System.Memory points to the netstandard2.0 folder. If you go to your packages folder for, you'll find that there are three flavors of this assembly: netcoreapp211, netstandard1.1, and netstandard2.0. I don't want the first one because I'm not using .NET Core, so I changed it to use 1.1 instead. After doing that for all assemblies that point to netstandard2.0 to 1.0 or 1.1 (whichever is available), VS then allowed me to use the System.Net.Http.4.3.4 package that I actually had installed.

I really don't know why this works, as I know almost nothing about .NETStandard, but it does.

like image 35
howcheng Avatar answered Feb 06 '23 05:02

howcheng