I recently created a .NET Core 2.0 library that multitargeted net46. It included this in the .csproj file:
<ItemGroup Condition="'$(TargetFramework)'=='net46'">
<Reference Include="System.Net.Http" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
<PackageReference Include="System.Net.Http" Version="4.3.3" />
<PackageReference Include="System.Net.Primitives" Version="4.3.0" />
<PackageReference Include="System.Net.Requests" Version="4.3.0" />
<PackageReference Include="System.Net.Sockets" Version="4.3.0" />
</ItemGroup>
I referenced this library is a .NET Framework 4.7 console app and get this exception:
Unhandled Exception: System.IO.FileNotFoundException: 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. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
I didn't see any warnings in the error list, but I decided to check the build log and found a conflict.
This is what I see in the detailed build log:
1> 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.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
1> "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.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" was not.
1> References which depend on "System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" [C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7\System.Net.Http.dll].
1> C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7\System.Net.Http.dll
1> Project file item includes which caused reference "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7\System.Net.Http.dll".
1> System.Net.Http
My App.config includes a redirect which should have been used
<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>
What I don't fully understand in why System.Net.Http includes a reference to itself. Is this corruption of some sort or am I missing something?
System.Net.Http.dll 4.1.1.0 shipped in nuget package System.Net.Http 4.3.0. It caused a problem on .NET Framework - see dotnet/runtime#18280 for details.
We shipped updated package System.Net.Http 4.3.1 which passes through to the underlying platform on .NET Framework (with assembly version 4.0.0.0).
I assume something in your app is referencing older nuget package with higher assembly version. You need a binding redirect like this:
<?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.1.1.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
@karelz The issue is that I do not use System.Net.Http 4.3.0 anywhere. My multi target for netstandard2.0 uses version 4.3.3 and the reference for net46 directly uses an assembly reference <Reference Include="System.Net.Http" />.
Also, the project that uses the library already has the binding redirect you mentioned.
<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>
EDIT: Nevermind I didn't notice the newVersion in your post was different than mine. Your fix seems to work for me.
I vaguely remember encountering this issue in the past; it might help future users to maintain a wiki page for this issue with enough keywords that it shows up in a google search.
Thanks for the help!
In my case the team was using Visual Studio 2019. I was using VS 2015 because that is what the sln would default to. Once I opened in in VS 2019 it worked. I guess that's because of the references in one applicaiton vs. the other. And actually in my case it was the System.Net.Sockets file.
Most helpful comment
System.Net.Http.dll 4.1.1.0 shipped in nuget package System.Net.Http 4.3.0. It caused a problem on .NET Framework - see dotnet/runtime#18280 for details.
We shipped updated package System.Net.Http 4.3.1 which passes through to the underlying platform on .NET Framework (with assembly version 4.0.0.0).
I assume something in your app is referencing older nuget package with higher assembly version. You need a binding redirect like this: