AssemblyBinding的BindingRedirect在使用T4MVCExtensions的MVC4应用程序中无法正常工作。

6
我们有一个大型的.NET Web应用程序,最初是使用MVC3和T4MVC开发的。最近我们将其升级到了MVC4,一切都很顺利,只是T4MVCExtensions(来自NuGet的版本3.6.5)仍然依赖于System.Web.Mvc版本3。根据web.config中应该设置的运行时依赖关系,它们应该会重定向程序集绑定,但实际上并没有。导致出现了以下异常情况:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

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.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

我们在web.config文件中有以下内容,应该可以进行映射,但似乎并未实现。
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Helpers"
                      publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc"
                      publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="4.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages"
                      publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
  </dependentAssembly>
</assemblyBinding>

我已经启用了融合日志,只是为了验证我的疯狂,并且看到确实它似乎在尝试使用mvc3二进制文件,而不是转向4。
*** Assembly Binder Log Entry  (5/21/2013 @ 3:37:23 PM) ***

The operation failed.
Bind result: hr = 0x80004005. Unspecified error

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable  c:\windows\system32\inetsrv\w3wp.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = IIS APPPOOL\ASP.NET v4.0
LOG: DisplayName = System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
 (Fully-specified)
LOG: Appbase = file:///C:/Work/Webs/SSUApp/SSUApp/
LOG: Initial PrivatePath = C:\Work\Webs\SSUApp\SSUApp\bin
LOG: Dynamic Base = C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\lssapp\7a6e6cde
LOG: Cache Base = C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\lssapp\7a6e6cde
LOG: AppName = d5bcf9a8
Calling assembly : App_Web__managerulespartial.cshtml.38bccb18.jb5ozz39, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: Start validating all the dependencies.
LOG: [Level 1]Start validating native image dependency mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
LOG: [Level 1]Start validating IL dependency System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
LOG: [Level 1]Start validating IL dependency System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a.
LOG: [Level 1]Start validating native image dependency System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
LOG: [Level 1]Start validating native image dependency System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
LOG: [Level 1]Start validating IL dependency System.Web.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
LOG: [Level 1]Start validating IL dependency System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
LOG: [Level 1]Start validating IL dependency System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
LOG: [Level 1]Start validating IL dependency System.Runtime.Caching, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a.
LOG: [Level 1]Start validating IL dependency Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
LOG: [Level 1]Start validating IL dependency System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
LOG: [Level 1]Start validating IL dependency System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
LOG: [Level 1]Start validating IL dependency System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
LOG: [Level 1]Start validating IL dependency System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
Native image has correct version information.
LOG: Validation of dependencies succeeded.
LOG: Bind to native image succeeded.
Attempting to use native image C:\Windows\assembly\NativeImages_v4.0.30319_64\System.Web.Mvc\f234ba2bcf5f845279e46dc04198a7cd\System.Web.Mvc.ni.dll.
Rejecting code sharing because a dependent assembly did not match the conditional APTCA share mode
Native image successfully used.

任何想法都可以,因为我已经在这上面花了很多时间了。
2个回答

12

我遇到了一个非常类似的程序集绑定问题,这是在手动将一组MVC3项目升级到MVC4之后发生的。尽管在web.config中具有程序集绑定元素,但我仍然会收到类似于T4MVC错误的错误,但来自Telerik MVC扩展。

融合日志查看器很有用,因为它确认应用程序域中加载了两个版本的system.web.mvc。

最终,从项目根web.config的根配置元素中删除命名空间解决了该问题。

因此,web.config如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<!--... elements deleted for clarity ...-->
</configuration>

移除命名空间后,它看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!--... elements deleted for clarity ...-->
</configuration>

这篇博客文章提供了关于错误命名空间的更多信息: http://www.dotnetish.com/runtime/xmlnshttpschemas-microsoft-com-netconfigurationv2-0/


2
先生,您赢了!那正是它的意思。非常感谢您。 - Francis Spor

0

为了隔离问题,我建议创建一个小的虚拟程序集,使用MVC3构建,看看它是否表现类似。T4MVC助手程序集应该没有什么特别之处。

此外,请查看我的测试项目https://github.com/davidebbo/T4MVCSampleApp。这是一个使用最新的T4MVC的MVC4项目,对我来说运行良好。你能否也运行成功?

如果所有其他方法都失败了,如果您可以分享一个可重现的项目(例如在github上),我可以尝试查看。


谢谢你的回应,David。我尝试了隔离文件,当然它完美地解决了问题。实际上,我甚至在我的解决方案中开始了一个新的 Web 应用程序项目,并且它也重新绑定得非常完美。自从我提交了 bug 之后,我还遇到了更新的 WebGrease.dll 出现了相同的问题,所以我认为 T4MVCExtension 在这种情况下是完全无辜的,我确信这只是我项目 web.config 中的错误配置。顺便说一句 - 我喜欢 T4MVC,它是框架的一个非常棒的补充,并且真正消除了使用 MVC 的猜测工作。非常感谢。 - Francis Spor

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