wpf 错误:无法加载文件或程序集“Microsoft.Practices.ServiceLocation,版本=1.0.0.0”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30870899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:12:40  来源:igfitidea点击:

Error: Could not load file or assembly 'Microsoft.Practices.ServiceLocation, Version=1.0.0.0

c#.netwpfmvvm

提问by Contango

I get this error:

我收到此错误:

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

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

How do I resolve this with an assembly redirect binding, if I already have another existing version of Microsoft.Practices.ServiceLocationin my project?

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

回答by Contango

One method is to recompile all NuGet packages to use the same version of Microsoft.Practices.ServiceLocation. At a pragmatic level, this just isn't practical: we need an easier method.

一种方法是重新编译所有 NuGet 包以使用相同版本的Microsoft.Practices.ServiceLocation. 从实用的角度来看,这并不实用:我们需要一种更简单的方法。

A better method is to use an assembly binding redirect. This works very nicely, if the interface is the same. This solution is tried and tested, and is running in production in a number of FTSEcompanies.

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

This is what the app.config looks like:

这是 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>

Adjust the target version to whatever version you already have, which is typically 1.2.0.0or 1.3.0.0.

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

The PublicKeyTokenmust match the target assembly. You can extract it using the following command:

PublicKeyToken必须在目标总成匹配。您可以使用以下命令提取它:

sn.exe -T assembly.dll

Example:

例子:

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>

For other ways of obtaining the PublicKeyToken, see Getting the PublicKeyToken of .Net assemblies.

对于其他获取方式PublicKeyToken,请参见获取 .Net 程序集的 PublicKeyToken

The PublicKeyTokendoes not change with the assembly version, e.g. its the same if the assembly is v1.0.0.0or v2.0.0.0.

PublicKeyToken不与程序集版本的变化,例如其同样如果大会v1.0.0.0v2.0.0.0