c# - 错误 : Could not load file or assembly 'Microsoft. Practices.ServiceLocation,版本 = 1.0.0.0

标签 c# .net wpf mvvm

我收到这个错误:

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,我该如何使用程序集重定向绑定(bind)解决此问题?

最佳答案

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

更好的方法是使用程序集绑定(bind)重定向。如果界面相同,则效果非常好。此解决方案经过反复测试,并在多个 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,则相同。

关于c# - 错误 : Could not load file or assembly 'Microsoft. Practices.ServiceLocation,版本 = 1.0.0.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30870899/

相关文章:

WPF TreeView 和虚拟化问题

c# - TryParse 特殊日期格式

c# - 预构建 MSBuild 任务以更新与构建的 exe 不同步的 AssemblyInfo

c# - 为什么字符串表现得像 ValueType

c# - ICollection<Person> 的返回类型是什么意思?

c# - 创建一个 IEqualityComparer<IEnumerable<T>>

c# - 如何将自闭标签添加到xml中的元素?

c# - 在没有基础字典结构的 PowerShell 中使用 C# ExpandoObjects(动态)

c# - 编码 .NET 泛型类型

C# MVVM DataGrid 绑定(bind)策略?