Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not load file or assembly "System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

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?

like image 568
Ivan-Mark Debono Avatar asked Jul 16 '16 05:07

Ivan-Mark Debono


People also ask

Can not load file or assembly?

In summary if you get the "Could not load file or assembly error", this means that either your projects or their references were built with a reference to a specific version of an assembly which is missing from your bin directory or GAC.

What is System Net HTTP?

HttpResponseMessage Class (System. Net. Http) Represents a HTTP response message including the status code and data.


2 Answers

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> 
like image 163
tripletdad99 Avatar answered Oct 05 '22 07:10

tripletdad99


Follow the following steps,

  1. Update visual studio to latest version (it matters)
  2. Remove all binding redirects from web.config
  3. Add this to the .csproj file:

    <PropertyGroup>   <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>   <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType> </PropertyGroup> 
  4. Build the project
  5. In the bin folder there should be a (WebAppName).dll.config file
  6. It should have redirects in it, copy these to the web.config
  7. Remove the above snipped from the .csproj file

It should work

like image 33
Sajeetharan Avatar answered Oct 05 '22 08:10

Sajeetharan