12

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?

Contango
  • 76,540
  • 58
  • 260
  • 305
  • This guy explained it perfectly, look at [this answer](http://stackoverflow.com/a/28789578/2014112) !! – KnowGe Mar 27 '16 at 20:08

1 Answers1

9

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.

Community
  • 1
  • 1
Contango
  • 76,540
  • 58
  • 260
  • 305
  • @Will, thanks for the edit. I usually put the disclaimer at the start "This is a Q&A style question", because I have had people vote me down as they do not understand what is going on. – Contango Jun 16 '15 at 21:33
  • 1
    Yeah, I figured as much. Feel free to do that in comments, btw! And consider that sometimes self-answered questions still are deserving of downvotes because people consider them poor questions... –  Jun 17 '15 at 14:02