Could not load file or assembly "System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
C#AzureVisual Studio-2015C# Problem Overview
I've copied my project to a clean Windows 10 machine with only Visual Studio 2015 Community and SQL Server 2016 Express installed. There are no other framework versions installed apart from those installed with Windows 10 and VS2015 or SQL Server.
When I try to start the WebApi project I get the message:
> 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.
The project's packages include:
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Tracing" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net45" />
After building the project with .NET Framework 4.6.1, System.Net.Http
the file is not found in the bin
folder.
The file's path points to:
> C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.6.1\System.Net.Http.dll
The file's path of System.Net.Http.Formatting
points to:
> C:\Development\MyApp\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll
Should the whole project target 4.5.1 or is there another way to reference the right assemblies?
C# Solutions
Solution 1 - C#
Changing the binding information in my web.config (or app.config) - while a "hack" in my view, allows you to move forward with your project after a NuGet package update whacks your application and gives you the System.Net.Http error.
Set oldVersion="0.0.0.0-4.1.1.0"
and newVersion="4.0.0.0"
as follows
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.0.0.0" />
</dependentAssembly>
Solution 2 - C#
Follow the following steps,
-
Update visual studio to latest version (it matters)
-
Remove all binding redirects from
web.config
-
Add this to the
.csproj
file:<PropertyGroup> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType> </PropertyGroup>
-
Build the project
-
In the
bin
folder there should be a(WebAppName).dll.config
file -
It should have redirects in it, copy these to the
web.config
-
Remove the above snipped from the
.csproj
file
It should work
Solution 3 - C#
In one of my projects there was a nuget packages with higher version of System.Net.Http. and in my startup project there reference to System.Net.Http v 4.0.0 , i just installed System.Net.Http nuget package in my startup project and the problem solved
Solution 4 - C#
Change following:
<bindingRedirect oldVersion="0.0.0.0-4.1.1.2" newVersion="4.1.1.2" />
with the following:
<bindingRedirect oldVersion="0.0.0.0-4.1.1.2" newVersion="4.0.0.0" />
in web.config
Solution 5 - C#
The above bind-redirect did not work for me so I commented out the reference to System.Net.Http
in web.config
. Everything seems to work OK without it.
<system.web>
<compilation debug="true" targetFramework="4.7.2">
<assemblies>
<!--<add assembly="System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />-->
<add assembly="System.ComponentModel.Composition, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
</assemblies>
</compilation>
<customErrors mode="Off" />
<httpRuntime targetFramework="4.7.2" />
</system.web>
Solution 6 - C#
If you have multiple projects in your solution, then right-click on the solution icon in Visual Studio and select 'Manage NuGet Packages for Solution', then click on the fourth tab 'Consolidate' to consolidate all your projects to the same version of the DLLs. This will give you a list of referenced assemblies to consolidate. Click on each item in the list, then click install in the tab that appears to the right.
Solution 7 - C#
This will work in .NET 4.7.2 with Visual Studio 2017 (15.9.4):
- Remove web/app.config binding redirects
- Remove NuGet package for System.Net.Http
- Open "Add New Reference" and directly link to the new 4.2.0.0 build that ships with .NET 4.7.2
Solution 8 - C#
You can fix this by upgrading your project to .NET Framework 4.7.2. This was answered by Alex Ghiondea - MSFT. Please go upvote him as he truly deserves it!
> This is documented as a known issue in .NET Framework 4.7.1.
>
> As a workaround you can add these targets to your project. They will
> remove the DesignFacadesToFilter from the list of references passed to
> SGEN (and add them back once SGEN is done)
>
>
Solution 9 - C#
4.6.1-2 in VS2017 users may experience the unwanted replacement of their version of System.Net.Http by the one VS2017 or Msbuild 15 wants to use.
We deleted this version here:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\System.Net.Http.dll
and here:
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\System.Net.Http.dll
Then the project builds with the version we have referenced via NuGet.
Solution 10 - C#
I have same issue and only way how i am able to fix it is add bindingRedirect to app.confing how wrote @tripletdad99.
But if you have solution with more project is really suck update every project by hand (and also sometimes after update some nuget package you need to do it again). And it is reason why i wrote simple powershell script which if all app.configs.
param(
[string]$SourceDirectory,
[string]$Package,
[string]$OldVersion,
[string]$NewVersion
)
Write-Host "Start fixing app.config in $sourceDirectory"
Write-Host "$Package set oldVersion to $OldVersion and newVersion $NewVersion"
Write-Host "Search app.config files.."
[array]$files = get-childitem $sourceDirectory -Include app.config App.config -Recurse | select -expand FullName
foreach ($file in $files)
{
Write-Host $file
$xml = [xml](Get-Content $file)
$daNodes = $xml.configuration.runtime.assemblyBinding.dependentAssembly
foreach($node in $daNodes)
{
if($node.assemblyIdentity.name -eq $package)
{
$updateNode = $node.bindingRedirect
$updateNode.oldVersion = $OldVersion
$updateNode.newVersion =$NewVersion
Write-Host "Fix"
}
}
$xml.Save($file)
}
Write-Host "Done"
Example how to use:
./scripts/FixAppConfig.ps1 -SourceDirectory "C:\project-folder" -Package "System.Net.Http" -OldVersion "0.0.0.0-4.3.2.0" -NewVersion "4.0.0.0"
Probably it is not perfect and also it will be better if somebody link it to pre-build task.
Solution 11 - C#
I had this, but, it was because I had added a NuGet package that had updated the binding redirects. Once I removed the package, the redirects were still there. I removed all of them, and then ran update-package -reinstall. This added the correct redirects.
Solution 12 - C#
Check .net framework version.
My original .net framework is older version.
After I installed .net framework 4.6, this issue is automatically solved.
Solution 13 - C#
For me, I had set my project to run on the latest version of .Net Framework (a change from .Net Framework 4.6.1 to 4.7.2).
Everything worked, no errors and published without issue, and it was only by chance that I came across the System.Net.Http error message, shown in a small, hard-to-notice, but quite important API request over the website I'm working on.
I rolled back to 4.6.1 and everything is fine again.
Solution 14 - C#
The only way that cleanly solved this issue for me (.NET 4.6.1) was to not only add a Nuget reference to System.Net.Http V4.3.4 for the project that actually used System.Net.Http, but also to the startup project (a test project in my case).
(Which is strange, because the correct System.Net.Http.dll existed in the bin directory of the test project and the .config assemblyBingings looked OK, too.)
Solution 15 - C#
Was updating an old website using nuget (including .Net update and MVC update).
I deleted the System.Net.HTTP reference in VS2017 (it was to version 2.0.0.0) and re-added the reference, which then showed 4.2.0.0.
I then updated a ton of 'packages' using nuget and got the error message, then noticed something had reset the reference to 2.0.0.0, so I removed and re-added again and it works fine... bizarre.
Solution 16 - C#
For me, I added the nuget again and the problem was solved
Solution 17 - C#
Removing dependentAssembly for name="System.Net.Http" from web.config also worked for me. I commented this part from web.config and it worked for me. If all the above solutions didn't worked for you try commenting or removing as show below.
<!--<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.1.1.2" newVersion="4.0.0.0"/>
</dependentAssembly>-->
Solution 18 - C#
Before doing tricks and configuration complexities, try deleting the bin and obj folders, then compile. That fixed same problem