Could not load file or assembly System.Web.Http.WebHost after published to Azure web site

181,698

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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.

Solution 5

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

Share:
181,698
ca9163d9
Author by

ca9163d9

Updated on July 08, 2022

Comments

  • ca9163d9
    ca9163d9 almost 2 years

    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
    ca9163d9 over 10 years
    The Copy Local is already True. Strangely it shows the Runtime Version is v4.0.30319 instead of v5?
  • Glenn Gordon
    Glenn Gordon over 9 years
    Do you know what happened here? I had been running fine for 18 months when this jumped up and bit me.
  • Eliyahu
    Eliyahu over 9 years
    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.
  • edgarpetrauskas
    edgarpetrauskas over 9 years
    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?
  • Naveen Vijay
    Naveen Vijay over 9 years
    I haven't tried if this would succeed. Can you take a look at nuget to update the libraries. - nuget.org
  • Anvesh Yalamarthy
    Anvesh Yalamarthy over 9 years
    Please avoid link only answers.. rather please post relevant information from above link here..
  • S'pht'Kr
    S'pht'Kr over 9 years
    @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.
  • pabloelustondo
    pabloelustondo about 9 years
    same here, this is exactly what was needed.
  • DigiOz Multimedia
    DigiOz Multimedia about 9 years
    Running the Update-Package command resolved the issue, whereas none of the other suggestions worked. Thank you for the answer!
  • dellyjm
    dellyjm about 9 years
    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?
  • Bronek
    Bronek about 9 years
    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.
  • Evilripper
    Evilripper about 9 years
    This resolved the problem on my VPS (this is not only azure problem)
  • Ohad Schneider
    Ohad Schneider about 9 years
    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!
  • TheQ
    TheQ about 9 years
    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.
  • raddevus
    raddevus almost 9 years
    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.
  • Festim Cahani
    Festim Cahani over 8 years
    Best solution to the problem.
  • ArtHare
    ArtHare over 8 years
    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.
  • Apolo
    Apolo over 8 years
    Perfect answer, Thanks !
  • Mungoid
    Mungoid over 8 years
    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.
  • Joseph King
    Joseph King over 8 years
    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.
  • DeeArgee
    DeeArgee over 8 years
    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.
  • jade290
    jade290 over 8 years
    Thank you so much! 2 days on this!
  • Debbie A
    Debbie A over 8 years
    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
  • skillworks
    skillworks almost 8 years
    This is the right answer. And very important is to close VS and delete .suo file and then restart VS.
  • Andiih
    Andiih almost 8 years
    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.
  • seangwright
    seangwright almost 7 years
    @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!
  • Chad
    Chad over 6 years
    Probably because its the same solution described in several other answers a year earlier.
  • Optimax
    Optimax about 6 years
    Or just add Microsoft.AspNet.WebApi.WebHost package via nuget.
  • Serj Sagan
    Serj Sagan over 5 years
    This worked for my but I also had to add Newtonsoft.Json and Localization
  • Sunil
    Sunil over 4 years
    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.