1
votes

I have just updated all packages in my application (which consists of many projects). One of these packages was System.Net.Http. Now I am getting an error:

'Storage' with identity 'Storage, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' uses 'System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' which has a higher version than referenced assembly 'System.Net.Http' with identity 'System.Net.Http, Version=4.1.1.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

I was hoping I could add a redirect, so in my Storage project, I put this:

<?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.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

in the app.config, but I still get the error. I guess my question is: Can I use a binding redirect for a referenced library or do I have to rollback my Update-Package and put version constraints on System.Net.Http??

2
Why not redirect to 4.2, i.e. <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />?Tsahi Asher
I tried that, didn't work.r3plica
You need to reference the v4.2 library of System.Net.Http, and set the newVersion to 4.2 as well.Zhaoxing Lu

2 Answers

2
votes

Ok, after hours of playing, looking around on the internet and solutions posted here, I finally got this to work. Here are the steps I did.

  1. Rename System.Net.Http.dll to System.Net.Http.dl_ in this folder: C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib
  2. For each project that references System.Net.Http, make sure they are using the NuGet package and not the one from extensions
  3. Build your project and look through each of the Refreences for each project, check the version of the System.Net.Http dll (right click > properties)
  4. Unload the project and search for System.Net.Http and make sure they are the same version as in step 3 (You can try setting them all to version 4.2.0.0, but I had issues with this)

Once this is done, trying rebuilding your project, it should build and should run. I hope this helps someone else.

1
votes

Steps for soloving Assembly uses version X which has a higher version than referenced assemblu error:

Open packages.config and delete the System.Net.Http entry.
Save and build then readd assemble from nuget.
Build and run.

When you compile apps in Visual Studio that target the .NET Framework 4.5.1 and later versions, binding redirects may be automatically added to the app configuration file to override assembly unification.

In Visual Studio, select the project in Solution Explorer, and then choose Open Folder in File Explorer from the shortcut menu. In File Explorer, find the project (.csproj or .vbproj) file and open it in Notepad.

Add the following element to the first configuration property group (under the tag):

<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

For more details, you could refer to this article.