45

I'm porting a Net Framework 4 dll to Net Core. When porting my unit tests project I get an exception running some specific tests (not all).

System.IO.FileLoadException: Could not load file or assembly 'System.Net.Http, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

This is the project.json for my dll

 {
  "version": "1.0.0-*",

  "dependencies": {
    "log4net": "2.0.7",
    "NETStandard.Library": "1.6.1",
    "Newtonsoft.Json": "9.0.1",
    "StackExchange.Redis": "1.2.1"
  },

  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50"
    }
  }
}

And this is Packages.config for the unit tests project

<packages>
  <package id="Castle.Core" version="4.0.0" targetFramework="net462" />
  <package id="log4net" version="2.0.7" targetFramework="net462" />
  <package id="Microsoft.Win32.Primitives" version="4.0.1" targetFramework="net462" />
  <package id="Moq" version="4.7.1" targetFramework="net462" />
  <package id="Newtonsoft.Json" version="9.0.1" targetFramework="net462" />
  <package id="StackExchange.Redis" version="1.2.1" targetFramework="net462" />
  <package id="System.Diagnostics.DiagnosticSource" version="4.0.0" targetFramework="net462" />
  <package id="System.IO" version="4.1.0" targetFramework="net462" />
  <package id="System.IO.FileSystem" version="4.0.1" targetFramework="net462" />
  <package id="System.IO.FileSystem.Primitives" version="4.0.1" targetFramework="net462" />
  <package id="System.IO.FileSystem.Watcher" version="4.0.0" targetFramework="net462" />
  <package id="System.Linq" version="4.1.0" targetFramework="net462" />
  <package id="System.Net.Http" version="4.1.1" targetFramework="net462" />
  <package id="System.Net.NameResolution" version="4.0.0" targetFramework="net462" />
  <package id="System.Runtime" version="4.1.0" targetFramework="net462" />
  <package id="System.Runtime.Extensions" version="4.1.0" targetFramework="net462" />
  <package id="System.Security.Cryptography.Algorithms" version="4.2.0" targetFramework="net462" />
  <package id="System.Security.Cryptography.Encoding" version="4.0.0" targetFramework="net462" />
  <package id="System.Security.Cryptography.Primitives" version="4.0.0" targetFramework="net462" />
  <package id="System.Security.Cryptography.X509Certificates" version="4.1.0" targetFramework="net462" />
  <package id="System.Text.RegularExpressions" version="4.1.0" targetFramework="net462" />
  <package id="System.Threading.Thread" version="4.0.0" targetFramework="net462" />
</packages>
anaximander
  • 7,083
  • 3
  • 44
  • 62
Jawen
  • 1,416
  • 1
  • 14
  • 26

10 Answers10

42

Fixed it by updating System.Net.Http to 4.3.1

Jawen
  • 1,416
  • 1
  • 14
  • 26
  • 2
    Removed from where? – Maritim Jun 29 '17 at 22:44
  • 1
    I did not have to remove System.Diagnostics.DiagnosticSource, just updating System.Net.Http resolved it for me! – Jasper Risseeuw Aug 17 '17 at 12:36
  • 2
    Update via Nuget (search for "http") – oo_dev Dec 06 '17 at 20:44
  • After updating NuGet, don't forget to fix any binding redirects. – Brad Irby Dec 17 '17 at 16:40
  • 3
    I struggled to update the dll. Eventually I find the way: Remove the current dll from the References in the Solution Explorer. Then right-click the References and choose Manage NuGet Packages. In the Search area write: http. Pay attention that there is Microsoft.Net.Http, and System.Net.Http. Choose the System.Net.Http. When you choose it you can see the version of the dll in the right panel. Good luck!! – Asaf May 10 '18 at 10:53
31

I got the issue, and realized that it was because I had two different references. one reference was from my project library, and the second one was a dependency of a .Net Standard library, and therefore a Nuget package. The steps to solve were the following:

  • Remove the reference to System.Net.Http (project => add reference => remove the reference).
  • Keep the System.Net and System.Net.Http Nuget packages
  • Run Update-Package –reinstall System.Net.Http to bring back the reference.

Now it works again. :)

Jean
  • 4,911
  • 3
  • 29
  • 50
  • 2
    I had this issue, and this fixed it for me! Removed the references and reinstalled the nuget package from the console. Thanks! – Dennis Aug 22 '18 at 09:35
  • Perfect resolution to the issue. Thanks for providing clear steps. – nycdan Nov 01 '19 at 19:15
14

Go to the app.config and remove all the lines there that reference System.Net.Http.

Shiroy
  • 1,648
  • 1
  • 15
  • 22
4

My problem was that my service had a referenced assembly that had a reference to a newer version of System.Net.Http. I've resolved the issue by updating System.Net.Http in the service.

Grigory Bushuev
  • 843
  • 9
  • 27
4

I had that same issue referencing System.Net.Http version 4.2.0.0 in a .NET Framework 4.8 project.

Reading the Exception info carefully, I noticed that there was an entry named "FusionLog".
It stated there was a version redirection in the binding of System.Http.Net in my App.config file.

In App.config I found an XML-section like:

  <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.1.1.0" newVersion="4.1.1.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

Removing that whole assemblyBinding section did the trick.

lidqy
  • 1,891
  • 1
  • 9
  • 11
3

The best and easiest way to fix this issue, is with a binding redirect.

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 you version, for example.

Jacob Gaiski
  • 516
  • 3
  • 10
2

I had this problem, while I had 10 projects depending on each other. I fixed that by adding the version that it asked for in one of the projects that was dependent on. It was not needed for compilation, but it seems that adding it, fixed the version in other projects while restoring. So it was:

Could not load file or assembly 'System.Net.Http, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

Then:

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.

So I added "System.Net.Http": "4.1.1", in one project..

That actually fixed the problem while it restored 8 projects.

Assil
  • 572
  • 6
  • 21
2

And now the junior programmer solution.... double check that you are making the updates suggested by Jawen and Jean in Nuget Package Manager in the correct places too. Remember that you might have a solution with your project and a test project, and so double check to update both sets of References.

Brian H
  • 246
  • 1
  • 12
0

I tried out various solutions (removing the dependentAssembly OR specifying the binding redirect as well). None of them worked.

However, the only solution which worked for me was to explicitly set Specific Version for System.Net.Http (or whatever DLL giving you version issues) to False from Visual Studio.

enter image description here

Umar Topia
  • 241
  • 2
  • 5
0

This problem really drives me nuts! It' been years since it was first reported, but seems that it doesn't ever make the cut. I recently hit this snag while working on a solution in Visual Studio 2022.

The good news is, the workarounds mentioned in the Githut threads actually work. For more background info, this is a good starting place: ["Could not load System.Net.Http, Version=4.x.y.z"][1]

The solution is truly simple, all you need to do is add binding redirects. A couple of caveats:

  • Ensure you add binding redirects everywhere this nuget is referenced
  • Use the lowest assembly version, instead of the highest version. This was silly, but when I tried using the highest version, as I'd normally have, I still hit this problem
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="System.Net.Http" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
            <bindingRedirect oldVersion="0.0.0.0-4.6.26011.1" newVersion="4.2.0.0" />
          </dependentAssembly>
      </runtime>
    </configuration>
  • You may also need to add the following to your project file, but in my case, it wasn't necessary:
    <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    [1]: https://github.com/dotnet/standard/issues/891
Gustavo Mori
  • 8,319
  • 3
  • 38
  • 52