Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.0, Elmah.MVC issue

Locally – my MVC 4, asp.net, c# app runs fine on IIS 8 / Windows 8.

When deployed to Windows Server 2008, I get this error:

Could not load file or assembly 'System.Web.Mvc, Version=3.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)

and

[FileLoadException: Could not load file or assembly 'System.Web.Mvc, Version=3.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)]
   Elmah.Mvc.Bootstrap.Initialize() +0

[InvalidOperationException: The pre-application start initialization method Initialize on type Elmah.Mvc.Bootstrap threw an exception with the following error message: Could not load file or assembly 'System.Web.Mvc, Version=3.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.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection`1 methods, Func`1 setHostingEnvironmentCultures) +12881963
   System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +12881672
   System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath) +240
   System.Web.Compilation.BuildManager.ExecutePreAppStart() +152
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +1151

[HttpException (0x80004005): The pre-application start initialization method Initialize on type Elmah.Mvc.Bootstrap threw an exception with the following error message: Could not load file or assembly 'System.Web.Mvc, Version=3.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) +12881108
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +159
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +12722297

This happens if I select ‘Only files needed to run this application’ from the ‘Items to deploy’ drop down in project properties/package/publish web.

If I select ‘all files in this project’ it works fine.

I guess Elmah is reliant on an older version of MVC or something – how can I fix this without having to upload all the files?

Whats the best way to problem solve situations like this?

Thanks.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

I had this exact same issue using MVC4 with Ninject built for .Net 4.5

To fix this i had to add a binding redirect to my Web.config file:
(at the end of the file, just before the </configuration> tag)

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <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="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0"/>
  </dependentAssembly>
    </assemblyBinding>
  </runtime>

This forces the web server to use System.Web.Mvc 4.0.0.0 instead of an older version.

Solution 2

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

Check correct versions are there

Solution 3

There are some procedures using to fix the issue and if the binding redirect in web.config does not solve the problem, you can try the following steps to fix it:

1) In Visual Studio Solution Explorer tree right-click References under your web project and select Manage NuGet Packages.

2) Go to Browse tab and select nuget.org as Package source.

3) Search and install the following packages: Ninject, Ninject.Web.Common and Ninject.MVC5.

It is also better to update the packages particularly Microsoft ASP.NET MVC on Updates tab of Manage NuGet Packages.

Hope this helps…

Solution 4

To install System.Web.Mvc 3.0.0.0 version

1) Install windows web platform installer
2) Open Windows web platform installer from start menu
3) Go to Products tab
4) Search for MVC
5) Install MVC 3

enter image description here

Solution 5

In the error page I had this:

LOG: Redirection detected in the application configuration file: 5.1.0.0 was redirected to 5.2.3.0.

So I had to change this line in the web.config to the 5.1.0.0 version and it worked!

<dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.1.0.0" />
    <!--<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> Older line -->
  </dependentAssembly>

I think this is due to a version problem when I downloaded the code from TFS

Hope this helps

Solution 6

Add below code in web.config:

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.1" />
  </dependentAssembly>
</assemblyBinding>

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply