Could not load file or assembly 'System.Web.Http 4.0.0 after update from 2012 to 2013

C#asp.net Web-ApiOdataVisual Studio-2013

C# Problem Overview


I did the upgrade according to. http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2

I get the error. Does any one else got this error?

> Server Error in '/' Application. > -------------------------------------------------------------------------------- > > > Could not load file or assembly 'System.Web.Http, Version=4.0.0.0, > Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its > dependencies. The located assembly's manifest definition does not > match the assembly reference. (Exception from HRESULT: 0x80131040)
> Description: An unhandled exception occurred during the execution of > the current web request. Please review the stack trace for more > information about the error and where it originated in the code. > > Exception Details: System.IO.FileLoadException: Could not load file > or assembly 'System.Web.Http, Version=4.0.0.0, Culture=neutral, > PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The > located assembly's manifest definition does not match the assembly > reference. (Exception from HRESULT: 0x80131040) > > Source Error: > > > > Line 48: Line 49: Line 50: } Line > 51: Line 52: protected void Application_BeginRequest(object > sender, EventArgs e) >
> > Source File: > d:\SM\7.4Test\WebAPIServerV2\ThirdPartyWebAPI\Global.asax.cs Line: > 50 > > Assembly Load Trace: The following information can be helpful to > determine why the assembly 'System.Web.Http, Version=4.0.0.0, > Culture=neutral, PublicKeyToken=31bf3856ad364e35' could not be loaded. > > > > === Pre-bind state information === LOG: DisplayName = System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 > (Fully-specified) LOG: Appbase = > file:///D:/SM/7.4Test/WebAPIServerV2/ThirdPartyWebAPI/ LOG: Initial > PrivatePath = D:\SM\7.4Test\WebAPIServerV2\ThirdPartyWebAPI\bin > Calling assembly : ThirdPartyWebAPI, Version=1.0.0.0, Culture=neutral, > PublicKeyToken=null. > === LOG: This bind starts in default load context. LOG: Using application configuration file: > D:\SM\7.4Test\WebAPIServerV2\ThirdPartyWebAPI\web.config LOG: Using > host configuration file: > C:\Users\michalc\Documents\IISExpress\config\aspnet.config LOG: Using > machine configuration file from > C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config. > LOG: Post-policy reference: System.Web.Http, Version=4.0.0.0, > Culture=neutral, PublicKeyToken=31bf3856ad364e35 LOG: Attempting > download of new URL > file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary > ASP.NET Files/root/9184b2ea/c6d4b139/System.Web.Http.DLL. LOG: > Attempting download of new URL > file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary > ASP.NET > Files/root/9184b2ea/c6d4b139/System.Web.Http/System.Web.Http.DLL. LOG: > Attempting download of new URL > file:///D:/SM/7.4Test/WebAPIServerV2/ThirdPartyWebAPI/bin/System.Web.Http.DLL. > WRN: Comparing the assembly name resulted in the mismatch: Major > Version ERR: Failed to complete setup of assembly (hr = 0x80131040). > Probing terminated. > >
> > Stack Trace: > > > > [FileLoadException: Could not load file or assembly 'System.Web.Http, > Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or > one of its dependencies. The located assembly's manifest definition > does not match the assembly reference. (Exception from HRESULT: > 0x80131040)] ThirdPartyWebAPI.WebApiApplication.Application_Start() > in d:\SM\7.4Test\WebAPIServerV2\ThirdPartyWebAPI\Global.asax.cs:50 > > [HttpException (0x80004005): Could not load file or assembly > 'System.Web.Http, Version=4.0.0.0, Culture=neutral, > PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The > located assembly's manifest definition does not match the assembly > reference. (Exception from HRESULT: 0x80131040)]
> System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext > context, HttpApplication app) +9935033
> System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr > appContext, HttpContext context, MethodInfo[] handlers) +118
> System.Web.HttpApplication.InitSpecial(HttpApplicationState state, > MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172
> System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr > appContext, HttpContext context) +336
> System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr > appContext) +296 > > [HttpException (0x80004005): Could not load file or assembly > 'System.Web.Http, Version=4.0.0.0, Culture=neutral, > PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The > located assembly's manifest definition does not match the assembly > reference. (Exception from HRESULT: 0x80131040)]
> System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9913572 > System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) > +101 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest > wr, HttpContext context) +254


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18408

C# Solutions


Solution 1 - C#

I fixed this by reinstalling the NuGet package, which corrects broken dependencies. From the package manager, run:

Update-Package Microsoft.AspNet.WebApi -reinstall

Solution 2 - C#

To resolve the Could not load file or assembly 'System.Web.Http' error use NuGet to install the Web API 2.1 WebHost.

In solution explorer in the references right click and select manage nuget packages. (if not there install nuget)

enter image description here

In the manage NuGet Packages window on the left side click online then in top right search for Web Host and install Microsoft ASP.NET Web API 2.1 Web Host. (Once installed the install button will change to a green check)

After that the project will reload and when it's build again the error will be resolved and the project will debug and run. The error will be gone and you may be 'The resource cannot be found. Just append the url ( ex from localhost:52088/ to localhost:52088api/products )

Your question was good and helped me.

Hope this answer helps!

Solution 3 - C#

You need to add assembly redirects:

<configuration>

   ....

   <runtime>
      <assemblyBinding>
    <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
      </assemblyBinding>
   </runtime>

   ...

</configuration>

Most likely you have to do this for a few more assemblies like webhosting, etc.

Solution 4 - C#

Or you could do this from NuGet Package Manager Console

 Install-Package Microsoft.AspNet.WebApi -Version 5.0.0

And then you will be able to add the reference to System.Web.Http.WebHost 5.0

Solution 5 - C#

When I installed Web Api 2.2 help this error started. I added this to the web.config and it solved my problem.

  <dependentAssembly>
    <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
  </dependentAssembly>

Solution 6 - C#

The proper solution to resolve this issue is by following the steps

. Update Visual studio if you have older version to 15.5.4 (Optional)

  1. Remove all binding redirects from web.config

  2. Add this to the csproj file:

> > true > true >

  1. Build.

  2. In the bin folder, there should be a (WebAppName).dll.config file.

  3. It should have redirects in it. Copy these to the web.config

  4. Remove the above snipped from the csproj file again

  5. It should work

Detailed Blog

Solution 7 - C#

I simply set Copy Local = true on the missing assembly and that worked. This of course happens by right clicking on the missing reference (which is stated in the error message) and selecting properties; as below:

enter image description here

This should then solve one problem as there might be more than one assembly causing the issue - what I did is I selected all assemblies and right click on them; selected properties; and set them all to Copy Local = true; and then republish.

Solution 8 - C#

For me it was only crashing with this error on a production environment, not on local machine; what solved it was to delete the content of /bin folder and then to regenerate it again.

Solution 9 - C#

Just an obvious but possible helpful hint....remember to check that the new version you specify in your webconfig assembly binding is the same version that you reference in your project references. (ie as I write this...this would be 5.1.0.0 if you have recently done a NUGet on System.Web.Http

Solution 10 - C#

<dependentAssembly>
    <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
<dependentAssembly>
    <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>

Solution 11 - C#

 Installing NuGet package ASP.NET.MVC solved the issue for me. 
 To install package.
 - Right click on project 
 - Select "Manage NuGet Packages"
 - Select Online->Nuget.org from left menu
 - select "Microsoft ASP.NET MVC"
 - Click Install

 If you have already installed package earlier, please update the package by selecting Updates from left menu and then selecting "Microsoft ASP.NET" and click Update.

enter image description here

enter image description here

Solution 12 - C#

This might help someone.

I decided to go with MVC4 app.

run project, worked

Then Installed few packages like,
Microsoft ASP.NET WEB API 2.2 (4 packages)
Microsoft ASP.NET WEB API 2.2 Help Page
Microsoft ASP.NET WEB API 2.2 OWIN

run project, same error as mentioned in question

Solution:

In web.config

 <dependentAssembly>
      <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.2.3.0" />
 </dependentAssembly>


Problem Solved


but then, some hotfix error found, To resolve it, go to VIEW folder of your MVC4 project, find second web.config file and changed version from 2.0.0.0 to 3.0.0.0 as shown below.


<configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>

Solution 13 - C#

In my case, I was actually missing my web.config altogether, which had the appropriate binding redirects. Restoring the web.config resolved the error.

Solution 14 - C#

I could solve similar problem with System.Net.HTTP by adding assembly reference in app.config of main application.

  <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.1" newVersion="4.0.0.0" />
      </dependentAssembly></runtime>

Solution 15 - C#

I found that Deleting the BIN folder and then rebuilding the project fixed the issue without having to uninstall/reinstall packages.

Solution 16 - C#

I found the solution ! Delete folder named package in project directory and then rebuild to run it.

Solution 17 - C#

YES!!!

Install-Package Microsoft.AspNet.WebApi -Version 5.0.0

It works fine in my case....thnkz

Solution 18 - C#

Me did not nothing, just copied development Bin folder DLLs to online deployed Bin folder and it worked fine for me.

Solution 19 - C#

I got this issue when deploying to Azure using the Publish feature. Remember to clear files at destination.

Publish Settings -> File Publish Options drop down -> Check Remove additional files at destination

This solved my issue, in case people have to hunt around for this like I did. Everything was the same version in my project/solution, just not at the destination I was deploying to.

Solution 20 - C#

I had the same problem with System.Web.Http.WebHost, Version=5.2.6.0 being referenced but the latest NuGet package was 5.2.7.0. I edited the web.config files, re-installed the NuGet package, then edited the visual studio project files for all of my projects to make sure no references to 5.2.6.0 persisted. Even after all this, the problem persisted.

Then I looked in the bin folder for the project that was throwing the exception, where I found a DLL for one of my other projects that is not a dependency and should never have been there. I deleted the offending DLL (which had been compiled using the 5.2.6.0 version of System.Web.Http.WebHost), rebuilt the troublesome project, and now it is working.

Solution 21 - C#

As others have said just reinstall the MVC package to your web project using nuget, but be sure to add the MVC package to any projects depending on the web project, such as unit tests. If you build each included project individually, you will see witch ones require the update.

Solution 22 - C#

I had same problem and reinstalling package doesn't work.

My application runs in LocalIIS(not IIS EXPRESS). I enable Assembly Load Trace and find out it want load System.Web.Http load from below temp catche. so remove all catch folders in it.

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files[my application]

Solution 23 - C#

I solved this problem by reverting the changes that nuget had made to my web.config after running nuget. Revert the changes to a previous working version.

  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
  </dependentAssembly>

Solution 24 - C#

What solved the problem for me was re-installing the Microsoft ASP.NET Web API 2.2 Help Page and Microsoft ASP.NET Web API 2.2 OWIN packages. I re-installed both at the same time, but I think it was the former that fixed the issue.

I tried the first few of the solutions offered here but that didn't help, then I compared my dependencies with the ones in a project that worked (from a course on Pluralsight) and the two dependencies above were a lower version (5.0.0) so I updated them to 5.2.3 and it started working.

Solution 25 - C#

I had this from a WCF service. For me (as the issue was displaying in local dev), I simply had to delete the contents of the bin folder under the solution. The the rebuild work fine once again.

Solution 26 - C#

ASP.NET Version:4.0.30319.18408 belongs to .Net4.5 and System.Web.Http Version=4.0.0.0 is compatible for .NET4.0. So the versions that you have are not compatible. You should update you System.Web.Http to version 5.0.0.0, which is compatible with .Net4.5

Solution 27 - C#

I had a similar issue. Everything was working before. It's was originally made using Web Forms and later on added Web API for some things.

  • Tried uninstalling, installing and reinstalling Microsoft.AspNet.WebApi package.
  • Tried removing packages folder from solution and letting NuGet restore in VS.
  • Tried removing individual assembly references (under References) and reinstalling packages.
  • Tried adding binding redirects to 5.2.3.0 version in web.config mentioned in several answers. Nothing worked for me.

What worked was, in Visual Studio, change publish profile settings from Release to Debug. Strange, but that's what worked. So sharing here.

Solution 28 - C#

If you use several projects on a solution, and call method in one project to another project, make sure that all projects (called project and caller project) use the unique 'System.Net.Http' version.

Solution 29 - C#

Change the Specfic version flag from true to false for the dll in the reference

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichal Asaf CarmeliView Question on Stackoverflow
Solution 1 - C#PathoschildView Answer on Stackoverflow
Solution 2 - C#CattoView Answer on Stackoverflow
Solution 3 - C#AliostadView Answer on Stackoverflow
Solution 4 - C#Sameer AlibhaiView Answer on Stackoverflow
Solution 5 - C#jhildenView Answer on Stackoverflow
Solution 6 - C#SajeetharanView Answer on Stackoverflow
Solution 7 - C#t_plusplusView Answer on Stackoverflow
Solution 8 - C#OanaView Answer on Stackoverflow
Solution 9 - C#Jim TaliadorosView Answer on Stackoverflow
Solution 10 - C#ravula sandeepView Answer on Stackoverflow
Solution 11 - C#Arvind KrmarView Answer on Stackoverflow
Solution 12 - C#Nikhil ShahView Answer on Stackoverflow
Solution 13 - C#Brandon HawbakerView Answer on Stackoverflow
Solution 14 - C#Parag BangadView Answer on Stackoverflow
Solution 15 - C#Jim TownsendView Answer on Stackoverflow
Solution 16 - C#Ashitosh birajdarView Answer on Stackoverflow
Solution 17 - C#X-CoderView Answer on Stackoverflow
Solution 18 - C#Anjan KantView Answer on Stackoverflow
Solution 19 - C#Shelby115View Answer on Stackoverflow
Solution 20 - C#cghoreView Answer on Stackoverflow
Solution 21 - C#DeeArgeeView Answer on Stackoverflow
Solution 22 - C#MohammadrezaView Answer on Stackoverflow
Solution 23 - C#roo2View Answer on Stackoverflow
Solution 24 - C#lukegfView Answer on Stackoverflow
Solution 25 - C#DubManView Answer on Stackoverflow
Solution 26 - C#oscarMgView Answer on Stackoverflow
Solution 27 - C#joym8View Answer on Stackoverflow
Solution 28 - C#MohammadSooriView Answer on Stackoverflow
Solution 29 - C#user2992715View Answer on Stackoverflow