【问题标题】:Assembly binding redirect for Unity not workingUnity 的程序集绑定重定向不起作用
【发布时间】:2012-09-01 18:28:41
【问题描述】:

我正在创建一个新的 MVC 4 网站,我想使用 Unity.MVC3 库与 MVC 中内置的 DependencyResolver 内容集成。

我还想从一个旧的、更大的项目中引用一些数据访问 DLL。

我的问题是 Unity.MVC3 和较旧的 DLL 分别针对不同版本的 Unity 1.2.0.0 和 2.1.505.0 进行编译。我尝试在我的 web.config 文件中创建绑定重定向,如下所示:

  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Practices.Unity" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersions="1.2.0.0-2.1.505.0" newVersion="2.1.505.0" />
  </dependentAssembly>

但是,我仍然收到以下错误:

 Could not load file or assembly 'Microsoft.Practices.Unity, Version=1.2.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)

我打开了程序集绑定日志,最后两行状态:

 WRN: Comparing the assembly name resulted in the mismatch: Major Version
 ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

为什么我的绑定重定向不被尊重?有没有办法覆盖它对主要版本冲突的检查?

【问题讨论】:

  • 你能在融合日志中看到绑定重定向是否真的被使用了吗?

标签: unity-container .net-assembly assembly-loading


【解决方案1】:

key token 有错别字:

<assemblyIdentity name="Microsoft.Practices.Unity" 
     publicKeyToken="31bf856ad364e35" />

应该是:

<assemblyIdentity name="Microsoft.Practices.Unity" 
     publicKeyToken="31bf3856ad364e35" />

绑定重定向does not complain in case of typos,它什么也不做。

我做了一个测试应用程序,使用这个配置它可以工作:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Practices.Unity" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.2.0.0" newVersion="2.1.505.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

注意xmlns,没有它会默默失败。

【讨论】:

  • 好收获!不过,似乎并没有解决问题。仍然收到错误。
猜你喜欢
  • 2011-03-30
  • 1970-01-01
  • 2012-04-27
  • 2017-02-16
  • 2017-09-09
  • 2019-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多