错误:无法加载文件或程序集' Microsoft.Practices.ServiceLocation,Version = 1.0.0.0

时间:2015-06-16 14:44:33

标签: c# .net wpf mvvm

我收到此错误:

  

无法加载文件或程序集' Microsoft.Practices.ServiceLocation,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'或其中一个依赖项。定位的程序集的清单定义与程序集引用不匹配。 (HRESULT异常:0x80131040)

如果我的项目中已有另一个现有版本的Microsoft.Practices.ServiceLocation,如何使用程序集重定向绑定解决此问题?

1 个答案:

答案 0 :(得分:8)

一种方法是重新编译所有NuGet包以使用相同版本的Microsoft.Practices.ServiceLocation。在务实的层面上,这是不切实际的:我们需要一种更简单的方法。

更好的方法是使用程序集绑定重定向。如果界面相同,这非常好用。该解决方案经过了试用和测试,并在许多FTSE公司中投入生产。

这就是app.config的样子:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <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.2.0.0" newVersion="1.2.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

将目标版本调整为您已有的版本,通常为1.2.0.01.3.0.0

PublicKeyToken必须与目标程序集匹配。您可以使用以下命令提取它:

sn.exe -T assembly.dll

示例:

C:\test>"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\x64\sn.exe" -T  C:\svn\lib\TargetDll.dll

Microsoft (R) .NET Framework Strong Name Utility  Version 4.0.30319.17929
Copyright (c) Microsoft Corporation.  All rights reserved.

Public key token is ac3efa7c033c2bd5
c:\test>

有关获取PublicKeyToken的其他方式,请参阅Getting the PublicKeyToken of .Net assemblies

PublicKeyToken不随汇编版本而变化,例如如果程序集为v1.0.0.0v2.0.0.0,则相同。