147

I created a web project and it runs well in Visual studio. However, I got the following error after published it to azurewebsites. What can cause the issue?

Could not load file or assembly 'System.Web.Http.WebHost, Version=5.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.WebHost, Version=5.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:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Assembly Load Trace: The following information can be helpful to determine why the assembly 'System.Web.Http.WebHost, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' could not be loaded.

WRN: Assembly binding logging is turned OFF. To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1. Note: There is some performance penalty associated with assembly bind failure logging. To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

The following is part of web.config file.

  <system.web>
    <customErrors mode="Off"/>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
  <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers></system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="DotNetOpenAuth.Core" publicKeyToken="2780ccd10d57b246" />
        <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="DotNetOpenAuth.AspNet" publicKeyToken="2780ccd10d57b246" />
        <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
ca9163d9
  • 27,283
  • 64
  • 210
  • 413

16 Answers16

131

The dll is missing in the published (deployed environment). That is the reason why it is working in the local i.e. Visual Studio but not in the Azure Website Environment.

Just do Copy Local = true in the properties for the assembly(System.Web.Http.WebHost) and then do a redeploy, it should work fine.

If you get the similar error i.e. some other assembly missing, then make that assembly to copylocal=true and redeploy, repeat this iteratively - if you are unsure of its dependencies.

Naveen Vijay
  • 15,928
  • 7
  • 71
  • 92
  • 2
    The `Copy Local` is already True. Strangely it shows the `Runtime Version` is v4.0.30319 instead of v5? – ca9163d9 Dec 03 '13 at 03:43
  • 4
    Do you know what happened here? I had been running fine for 18 months when this jumped up and bit me. – Glenn Gordon Oct 20 '14 at 21:29
  • I think a recent Windows update changed something. By me everything worked for years before starting giving the error today. Anyway setting Copy Local is a good solution for now. Eadel's answer didn't work for me. – Eliyahu Oct 21 '14 at 12:44
  • 2
    This fixed the problem for me Thanks! But it seems strange to me that we have to include framework libraries in our projects(In my case not Azure but a IIS server). Does anybody know if its a case of running some updates so we dont have to include them anymore? – edgarpetrauskas Oct 22 '14 at 08:31
  • I haven't tried if this would succeed. Can you take a look at nuget to update the libraries. - nuget.org – Naveen Vijay Oct 24 '14 at 13:53
  • @GlennGordon It's a hunch, but I think this happened to me because I installed .NET Framework 4.5 and ASP.Net 2…in a completely different project in VS Express 2013 (?!?). Then I went back to this older project, fixed a bug, and rebuilt and republished with VS2010. I think that this and 3 other DLLs I was missing (they didn't get built into my Release/bin folder on my workstation either!) are now in the GAC with .NET 4.5, or something like that, therefore the build system doesn't think they need to be included…but if you publish to a server without .NET 4.5, boom! Just a theory. – S'pht'Kr Dec 11 '14 at 21:49
  • I created an "Empty MVC 4" project in Visual Studio 2013 and it seems these DLLs are set incorrectly so they don't get copied over. Quite annoying. – raddevus Jun 18 '15 at 17:42
  • 1
    Mini add-on since it took me forever to find copy local: In VS2013, you open up the "references" node in the project, and right-click->properties on the library you want to set "copy local" on. – ArtHare Aug 27 '15 at 13:01
  • 6
    If Copy Local is already set to true and you cannot update the WebApi because of dependencies, there is that trick to set Copy Local to false, build, then set Copy Local back to true and build. I don't know why this works. – DeeArgee Nov 19 '15 at 17:16
  • 1
    @DeeArgee I've fought this issue in several projects and always solved it but I never knew what the black magic combination of setting toggles, rebuilds and saves were. Your suggestion worked the first time I tried it. Thanks! – seangwright Jul 20 '17 at 22:16
  • If it is still not working even after set the "CopyLocal=true" then other way around is to verify the dll version of all referenced assemblies which starts with System.Web.Http. Ideally all of them should be same, if any discrepancy found then refer the same assembly version for all of them. – Sunil Dec 05 '19 at 16:34
93

If you are still looking for an answer, try checking this question thread. It helped me resolve a similar problem.

edit: The solution that helped me was to run Update-Package Microsoft.AspNet.WebApi -reinstall from the NugGet package manager, as suggested by Pathoschild. I then had to delete my .suo file and restart VS, as suggested by Sergey Osypchuk in this thread.

Community
  • 1
  • 1
amraby
  • 1,228
  • 10
  • 11
  • Please avoid link only answers.. rather please post relevant information from above link here.. – Anvesh Yalamarthy Oct 29 '14 at 20:11
  • 3
    Running the Update-Package command resolved the issue, whereas none of the other suggestions worked. Thank you for the answer! – DigiOz Multimedia Feb 24 '15 at 16:33
  • Best solution to the problem. – Festim Cahani Aug 24 '15 at 14:11
  • Perfect answer, Thanks ! – Apolo Aug 27 '15 at 18:11
  • I think in most cases, Naveen has the correct answer. However in my case, i pulled down our solution without the packages folder and when i did the nuget command to reinstall from packages.conf, it must have changed how the dlls were pulled. All other dlls were fine but this one in particular was pointing to the .net folder. – Mungoid Oct 13 '15 at 14:15
  • 3
    This solution worked for me also. I'm fairly sure this has been caused by ReSharper's 'Remove Unused Assemblies' functionality. Remove unused Assemblies sometimes blindly removes assemblies without check NuGet package contents. – Joseph King Oct 16 '15 at 13:43
  • Thank you so much! 2 days on this! – jade290 Dec 02 '15 at 02:04
  • This is the right answer. And very important is to close VS and delete .suo file and then restart VS. – skillworks May 18 '16 at 13:57
  • Make sure you have a backup/scc in place before running this. It removed all packages then failed for me, giving me a solution with no packages in package manager! However, restoring from git and then rebuilding to reinstate packages did solve the issue. – Andiih Jul 14 '16 at 07:52
56

I met the same problem and I resolved it by setting CopyLocal to true for the following libs:

System.Web.Http.dll
System.Web.Http.WebHost.dll
System.Net.Http.Formatting.dll

I must add that I use MVC4 and NET 4

Eriawan Kusumawardhono
  • 4,796
  • 4
  • 46
  • 49
Bronek
  • 10,722
  • 2
  • 45
  • 46
  • thank you this was helpful. Do you know why these files wouldn't just be in GAC? Is it because different sites could be using different dotnet frameworks etc? – dellyjm Mar 05 '15 at 16:51
  • As I remember this issue has happened since Microsoft applied critical fixes in that area (I guess in System.Web/ASP NET/MVC). I guess these namespaces are not in GAC (so not in native NET assemblies) but in separate Visual Studio or MVC paths. – Bronek Mar 05 '15 at 21:49
  • This resolved the problem on my VPS (this is not only azure problem) – Evilripper Mar 16 '15 at 11:27
  • This worked for me (though I'm not using Azure). I was moving a project from a .net 4.5 framework environment down to a 4.0 one and got this issue at the end of everything. – TheQ Apr 30 '15 at 17:48
  • 2
    Per DeeArgee's suggestion above, I already had LOCAL COPY = true for all 3 of these dll's. But this suggestion finally solved the problem: "If Copy Local is already set to true, there is that trick to set Copy Local to false, build, then set Copy Local back to true and build. I don't know why this works. – DeeArgee Nov 19 '15 at 17:16 – Debbie A Jan 28 '16 at 07:26
  • This worked for my but I also had to add `Newtonsoft.Json` and `Localization` – Serj Sagan Oct 08 '18 at 06:06
34

For me worked adding the following section to web.config file:

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

This example stands for MVC 5.1. Hope it will help someone to resolve such issue.

Eadel
  • 3,797
  • 6
  • 38
  • 43
17

For me it started working after selecting "Remove additional files at destination" in File publish options under settings on the publish dialog.

Magnus Ahlin
  • 655
  • 1
  • 6
  • 18
  • This is the only solution here that worked for me. I guess there was some other old version of a dll there that tripped things up. Thanks! – Ohad Schneider Apr 23 '15 at 17:37
10

The dll is missing in the published (deployed environment). That is the reason why it is working in the local i.e. Visual Studio but not in the Azure Website Environment.

Just do Copy Local = true in the properties for the assembly(System.Web.Http.WebHost) and then do a redeploy, it should work fine.

Philip Pittle
  • 11,821
  • 8
  • 59
  • 123
venkat
  • 121
  • 1
  • 2
6

I'm using vs2012 and i think the update KB2781514 changed some setting. All my System.Web.Http in my MVC4 project changed to false and i keeping received this message. I had changed the All file in this project in publish property but its not working. Finally I have to change Copy Local = true one by one and solved this problem.

thanh
  • 426
  • 7
  • 16
2

I had the same problem in my Application.

System.web.http.webhost not found.

You just need to copy the system.web.http.webhost file from your main project which you run in Visual Studio and paste it into your published project bin directory.

After this it may show the same error but the directory name is changed it may be system.web.http. Follow same procedure as above. It will work after all the files are uploaded. This due to the nuget package in Visual Studio they download from the internet but on server it not able to download it.

You can find this file in your project bin directory.

Burgi
  • 421
  • 8
  • 24
imran khan
  • 474
  • 5
  • 10
2

I got the same error and I changed my version from 4 to 3 and it is solved:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <!-- Ensure correct version of MVC -->
    <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
    </dependentAssembly>
</assemblyBinding>
Burgi
  • 421
  • 8
  • 24
Ilaria
  • 167
  • 2
  • 13
1

This happened to me on VS2013(Update 5)/ASP.NET 4.5, under project type "Web Application" that includes MVC and Web API 2. Error happened right after creating the project and before adding any code. Adding the following configuration fix it for me. After resolving "System.Web.Helpers" issue two more similar errors surfaced for "System.Web.Mvc" and "System.Web.WebPages".

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
Masoud Safi
  • 102
  • 1
  • 6
0

I was missing several DLLs. Even if I manually copied them to the directory the next time I published they would disappear. Each one was already set to Copy Locally in VS. The fix for me was to set each one to Copy Locally false, save, build then set each one to copy locally true. This time when I published all of the DLLs published correctly. Strange

Grayson
  • 191
  • 1
  • 9
0

If you have multiple projects in your solution and one of your projects fails to build because of this error then ensure you have installed the WebApi Core nuget package in that project. Simply adding a reference to the System.Web.Http does not help, you need to install the correct nuget package into that project.

I had multiple projects in my solution and WebApi Core was already installed in another project. I referenced the System.Web.Http assembly by right-clicking and ticking the assembly from the list and it did not work on Azure, though locally it would build okay. I had to remove the manual reference and add the WebApi Core nuget package to each project that needed the assembly reference.

Ali
  • 1,462
  • 2
  • 17
  • 32
0

In the case if "Copy Local" is already True, I find it sometimes to work if you remove the files where it has been published to and publish again.

For example if you're using IIS, remove the websites and the contents of the directory to which they are published to, and publish again.

There might be older versions of files at the destination, so to ensure you aren't using older versions, delete everything before publishing again.

Prasanth Louis
  • 4,658
  • 2
  • 34
  • 47
0

I removed the following entry from web.config and it worked for me.

<dependentAssembly>
                <assemblyIdentity name="System.Web.Http.WebHost" culture="neutral" publicKeyToken="31BF3856AD364E35" />
                <bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="5.2.6.0" />
            </dependentAssembly>
0

Make sure the package version is equal across the solution. I just downgraded & upgraded Microsoft.AspNet.Mvc package across the solution and the problem solved.

Masoud Darvishian
  • 3,754
  • 4
  • 33
  • 41
0

In my case, this problem likely occurred after messing with nuget and getting it to reconcile versions.

I discovered that the system. Web.http.webhost versions were not the same in my development folder as in the deployed folder. All I did was delete the file, redeployed, and then it worked.

Mind you, I also set the reference to “copy local” as other people said here, but that didn’t work. I can’t say in the end if it was also a helping factor.