无法加载文件或程序集“System.Web.Mvc,版本= 3.0.0.0”,出现Elmah.MVC问题。

35

在本地,我的MVC 4、ASP.NET和C#应用程序在IIS 8 / Windows 8上可以很好地运行。

但是当部署到Windows Server 2008时,我会遇到这个错误:

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)

[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

如果我从项目属性/包/发布Web中的“要部署的项”下拉菜单中选择“仅需要运行此应用程序的文件”,就会出现这种情况。

如果我选择“此项目中的所有文件”,它就可以正常工作。

我猜Elmah依赖于旧版本的MVC或其他什么东西 - 我如何在不必上传所有文件的情况下解决这个问题?

像这样解决问题的最佳方法是什么?

谢谢。

6个回答

63

我在使用为 .Net 4.5 构建的 MVC4 和 Ninject 时遇到了完全相同的问题。

为了解决这个问题,我不得不在我的 Web.config 文件中添加绑定重定向: (在文件末尾,在</configuration> 标签之前添加)

  <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>

这将强制 Web 服务器使用 System.Web.Mvc 4.0.0.0 而不是较旧的版本。


使用asp.net mvc 5版本,将2.0.0.0替换为3.0.0.0,将4.0.0.0替换为5.0.0.0。 - live-love
1
但是为什么会发生这种情况呢?即使没有使用NInject,我仍然遇到了同样的问题,但我无法弄清楚哪个程序集在引用v3!FusionLogs显示“某人”正在请求它,但调用程序集是未知的。我真的非常想知道为什么。 - atomaras

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

检查是否有正确的版本


0
安装 System.Web.Mvc 3.0.0.0 版本
1) 安装Windows Web Platform Installer 2) 在开始菜单中打开 Windows Web 平台安装程序 3) 转到 产品 选项卡 4) 搜索 MVC 5) 安装 MVC 3

enter image description here


OP正在使用MVC4 - 安装MVC3会相当低效。 - PKD

0
有一些步骤可以用来解决这个问题,如果在web.config中的绑定重定向无法解决问题,您可以尝试以下步骤来修复它:
1)在Visual Studio解决方案资源管理器树中,右键单击您的Web项目下的引用,并选择管理NuGet程序包。
2)转到浏览选项卡,并选择nuget.org作为软件包源。
3)搜索并安装以下软件包:Ninject,Ninject.Web.Common和Ninject.MVC5。
最好还要在NuGet程序包管理器的更新选项卡上更新软件包,特别是Microsoft ASP.NET MVC。

-1
在错误页面中,我看到了这个信息:
LOG: 在应用程序配置文件中检测到重定向:5.1.0.0 被重定向到 5.2.3.0。
因此,我不得不将 web.config 文件中的这一行更改为 5.1.0.0 版本,然后它就可以正常工作了!
<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>

我认为这是因为我从TFS下载代码时出现了版本问题

希望这可以帮到你


-2

在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>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接