防止运行时绑定到旧程序集

4
我有一个包含3个项目的解决方案:
Solution.Web.Core - 通用类
Solution.Web.Mvc - MVC4
Solution.Web.Api - Web Api2,也就是运行ASP.NET 5.0(beta-1)程序集
我已经将WebApi设置为MVC应用程序的子应用程序(在IIS中),但是,我在实现该项目的完全隔离时遇到了问题。
因为Solution.Web.Core引用了一些ASP.NET 4.0程序集,如果我在Solution.Web.Api中添加对该项目的引用,则会在运行时收到以下异常:
无法加载文件或程序集“System.Net.Http.Formatting,Version = 5.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35”或其某个依赖项。所定位的程序集清单定义与程序集引用不匹配。(来自HRESULT的异常:0x80131040)
我该如何防止Solution.Web.Api中出现这个错误,以便我可以引用Solution.Web.Core,尽管Solution.Web.Core引用了较旧版本的ASP.NET程序集?
注意:由于错误提示说“System.Net.Http.Formatting或其某个依赖项”,我甚至不知道如何找出确切的有问题程序集。

你可以看一下这个:http://msdn.microsoft.com/zh-cn/library/w4atty68.aspx - Mike Cheel
谢谢。这似乎是针对.NET运行时的。我所有三个应用程序都在运行.NET 4.5。不同的是ASP.NET组件。 - parliament
我认为有一些设置(我认为那就是)可以让你指定你所询问的内容。 - Mike Cheel
2个回答

2
更改视图部分的Web.Config如下:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, **Version=5.0.0.0**, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
  <namespaces>

to

<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, **Version=5.2.3.0**, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
  <namespaces>

1
我通过重新使用MVC 5 + Web Api 2来解决了这个问题。
然而,我刚刚发现这篇博客文章的答案,它似乎可能对我有用:
为了让一个MVC 4项目能够与5.0 beta程序集(至少是博客文章中的CORS程序集)兼容,请将web config更改为以下内容:
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:bcl="urn:schemas-microsoft-com:bcl">
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" />
    <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
  </dependentAssembly>


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