Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

mvvm

wpf

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)

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

like image 268
Contango Avatar asked Jun 16 '15 14:06

Contango


1 Answers

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.

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 FTSE companies.

This is what the app.config looks like:

<?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.0 or 1.3.0.0.

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

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.

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

like image 98
Contango Avatar answered Nov 11 '22 04:11

Contango