一种方法是重新编译所有的NuGet包,以使用相同版本的
Microsoft.Practices.ServiceLocation
。但从实用的角度来看,这并不切实际:我们需要一种更简单的方法。
更好的方法是使用程序集绑定重定向。如果接口相同,这种解决方案非常有效。这个解决方案经过了尝试和测试,并且正在多家
富时股票指数公司的生产环境中运行。
以下是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.0
或1.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
的其他方法,请参见获取 .Net 程序集的 PublicKeyToken。
PublicKeyToken
不会随着程序集版本更改而改变,例如对于程序集 v1.0.0.0
或 v2.0.0.0
,它是相同的。