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

188

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#
asp.net-web-api
odata
visual-studio-2013
asked on Stack Overflow Oct 21, 2013 by Michal Asaf Carmeli • edited Oct 21, 2013 by Soner Gönül

29 Answers

297

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

Update-Package Microsoft.AspNet.WebApi -reinstall
answered on Stack Overflow Mar 7, 2014 by Pathoschild • edited Aug 29, 2017 by meJustAndrew
85

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!

answered on Stack Overflow Mar 13, 2014 by Catto • edited Oct 9, 2014 by Catto
61

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.

answered on Stack Overflow Oct 21, 2013 by Aliostad • edited Oct 26, 2013 by Aliostad
18

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

answered on Stack Overflow Dec 17, 2013 by Sameer Alibhai
17

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>
answered on Stack Overflow Sep 16, 2014 by jhilden
5

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:

 <PropertyGroup>
  <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
  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

answered on Stack Overflow Dec 29, 2017 by Sajeetharan
4

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.

answered on Stack Overflow Jul 4, 2016 by t_plusplus • edited Jul 4, 2016 by t_plusplus
3

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.

answered on Stack Overflow Feb 22, 2018 by Oana
2

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

answered on Stack Overflow Feb 17, 2014 by Jim Taliadoros
2
<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>
answered on Stack Overflow Mar 23, 2015 by ravula sandeep • edited Jul 28, 2015 by Vivek Jain
2
 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

answered on Stack Overflow Oct 15, 2015 by Arvind Krmar • edited Oct 15, 2015 by Arvind Krmar
2

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>

answered on Stack Overflow Feb 13, 2016 by micronyks
2

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

answered on Stack Overflow Jul 26, 2016 by Brandon Hawbaker • edited Jul 26, 2016 by KernelPanic
2

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>
answered on Stack Overflow Jul 19, 2017 by Parag Bangad
1

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

answered on Stack Overflow Jul 7, 2015 by Ashitosh birajdar
1

YES!!!

Install-Package Microsoft.AspNet.WebApi -Version 5.0.0

It works fine in my case....thnkz

answered on Stack Overflow Dec 7, 2015 by X-Coder
1

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

answered on Stack Overflow Jun 15, 2017 by Anjan Kant
1

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.

answered on Stack Overflow Sep 15, 2017 by Shelby115
1

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

answered on Stack Overflow Jan 8, 2021 by Jim Townsend
0

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.

answered on Stack Overflow Feb 9, 2015 by DeeArgee
0

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]

answered on Stack Overflow Mar 16, 2015 by Mohammadreza
0

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>
answered on Stack Overflow Jul 23, 2015 by roo2
0

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.

answered on Stack Overflow Dec 30, 2015 by lukegf
0

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.

answered on Stack Overflow Mar 9, 2016 by DubMan
0

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

answered on Stack Overflow Oct 12, 2016 by oscarMg
0

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.

answered on Stack Overflow Sep 12, 2017 by joym8
0

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.

answered on Stack Overflow Jun 27, 2018 by MohammadSoori
0

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

answered on Stack Overflow Sep 26, 2018 by user2992715
0

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.

answered on Stack Overflow Jun 29, 2019 by cghore

User contributions licensed under CC BY-SA 3.0