试图运行成功构建的MVC 5.2项目会出现以下错误:
An exception of type ‘System.IO.FileLoadException’ occurred in
ProjectNamespace.WebUI.dll but was not handled in user codeAdditional information: Could not load file or assembly ‘Autofac,
Version=3.4.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da’ or
one of its dependencies. The located assembly’s manifest definition
does not match the assembly reference. (Exception from HRESULT:
0x80131040)Assembly manager loaded from:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll Running under
executable C:\Program Files (x86)\IIS Express\iisexpress.exe
— A detailed error log follows.=== Pre-bind state information === LOG: DisplayName = Autofac, Version=3.4.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da
(Fully-specified) LOG: Appbase = file:///C:/ProjectPath/WebUI/ LOG:
Initial PrivatePath = C:\ProjectPath\WebUI\bin Calling assembly :
Autofac.Integration.Mvc, Version=3.3.0.0, Culture=neutral,
PublicKeyToken=17863af14b0044da.
=== LOG: This bind starts in default load context. LOG: No application configuration file found. LOG: Using host configuration file:
C:\Users\me\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: Autofac, Version=3.4.0.0, Culture=neutral,
PublicKeyToken=17863af14b0044da LOG: Attempting download of new URL
file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary
ASP.NET Files/root/5c8bedab/6a36492/Autofac.DLL. LOG: Attempting
download of new URL
file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary
ASP.NET Files/root/5c8bedab/6a36492/Autofac/Autofac.DLL. LOG:
Attempting download of new URL
file:///C:/ProjectPath/WebUI/bin/Autofac.DLL. WRN: Comparing the
assembly name resulted in the mismatch: Minor Version ERR: Failed to
complete setup of assembly (hr = 0x80131040). Probing terminated.
我的web.config具有以下设置:
<dependentAssembly>
<assemblyIdentity name="Autofac" publicKeyToken="17863af14b0044da" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.5.0.0" newVersion="3.5.0.0" />
</dependentAssembly>
花了好几个小时试图解决它,但失败了.请帮忙.
最佳答案 这个问题已经变得非常普遍.当您使用Autofac,Autofac.Mvc5和Microsoft.AspNet.Mvc> 5.1.0软件包时,似乎会出现这种情况,即使Autofac.Mvc5可用于Microsoft.AspNet.Mvc(≥5.1.0&& < 6.0.0).
>我建议为autofac检查和添加适当的程序集绑定重定向的经典解决方案,不仅在引用它的项目中,而且在引用该项目的项目中.
>另一个潜在的问题是有两个条目
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
在你的配置中,如果你在第二个绑定中将它们移动到第一个绑定.在我加密某些配置设置的情况下,我修复了它,并且由于某种原因,绑定重定向没有被复制到生成的配置文件中.
我强烈推荐Assembly Binding Log Viewer,它帮助我解决了这个问题,尽管我的最终解决方案是懦弱的,转到Autofac3.4.0.0和Microsoft.AspNet.Mvc 5.1.0.我花了太多时间来解决问题.
此外,对于那些感兴趣的人,您可以使用nuget管理器来检测并填写程序集绑定重定向:Get-Project -All | Add-BindingRedirect(从here开始).之后可能需要一些补充,这取决于你的项目和参考文献,但至少它会给你一个干净的石板开始.
更新
Microsoft.AspNet.Mvc 5.2.3发布,所以我决定再试一次.我将“冲突”中涉及的每个包更新到最新版本(Autofac 3.5.2,Autofac.Mvc5 3.3.4,Microsoft.AspNet.Mvc 5.2.3).我更新了重定向绑定并且它有效.