c# - 程序集绑定(bind) - 使用了错误版本的 Unity,但为什么呢?

标签 c# .net unity-container

在这个 Selenium 测试项目中,我直接引用了 Unity 3.0.0.0。

我有对引用 Unity 2.505.0.2 的其他 DLL 的引用。

我的 app.config 中有这个:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Practices.ServiceLocation" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="1.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Practices.Unity" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="AutoMapper" publicKeyToken="be96cd2c38ef1005" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.2.1.0" newVersion="3.2.1.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

我预计它会使用 Unity V3,但它一直在尝试使用旧版本。绑定(bind)日志为:

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 11.0\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\TESTWINDOW\vstest.executionengine.x86.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = INT\UKTHJHAR
LOG: DisplayName = Microsoft.Practices.Unity, Version=2.1.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
 (Fully-specified)
LOG: Appbase = file:///C:/Audatex/Retail/Development/Server/AudaRetailAdministration.AutomatedUITests/bin/Debug
LOG: Initial PrivatePath = NULL
Calling assembly : AudaRetailAdministration.AutomatedUITests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Audatex\Retail\Development\Server\AudaRetailAdministration.AutomatedUITests\bin\Debug\AudaRetailAdministration.AutomatedUITests.dll.config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Microsoft.Practices.Unity, Version=2.1.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: Attempting download of new URL file:///C:/Audatex/Retail/Development/Server/AudaRetailAdministration.AutomatedUITests/bin/Debug/Microsoft.Practices.Unity.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

最佳答案

我认为程序集绑定(bind)仅适用于直接引用。

可能是为了防止在单个应用程序(或应用程序域?)中使用同一程序集的不同版本

如果您使用由应用程序引用的另一个程序集版本编译的组件,则该组件的加载将失败。

如果您反编译引用旧版本 Unity 的程序集,您会看到 Unity 版本以某种方式硬编码在程序集元数据中。

这个帖子有同样的问题 Why is assembly binding redirect not working in my web site?

我确实通过在应用程序及其组件中使用相同的程序集版本解决了这个问题。这对你来说可能是不可能的。

此问题是使用 GAC 发布的复杂应用程序的缺点。现在,人们倾向于使用 nugget 包来轻松管理组件依赖关系。

关于c# - 程序集绑定(bind) - 使用了错误版本的 Unity,但为什么呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26276927/

相关文章:

c# - Ninject 和通用

wcf - 是否有用于为 Unity IOC 配置 WCF 应用程序的 Nuget 包?

c# - Unity - 解决依赖失败(没有注册)

.net - 异步任务方法的 TaskCreationOptions

c# - .net Reflection.Emit 简化框架?

.net - 如何确定给定的整数是否在特定的枚举中?

c# - Unity.MVC 无法使用参数解析我的 Controller 构造函数

c# - 未调用 ASP.NET 组合 selectedIndexChanged 事件

c# - ASP.net 中的 foreach 带有嵌套 runat ="server"

c# - 在 C# 中使用 @keyword——坏主意?