5

Even though my references have Specific Version set to false, I'm getting assembly binding errors because the target machine has a higher version. How do I specify the current version or higher to avoid the following error when some target machines might have version 1.61.0.0 while others have 1.62.0.0 or higher?

System.IO.FileLoadException: Could not load file or assembly 'ServerInterface.NET, Version=1.61.0.0, Culture=neutral, PublicKeyToken=151ae431f239ddf0' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'ServerInterface.NET, Version=1.61.0.0, Culture=neutral, PublicKeyToken=151ae431f239ddf0'
flipdoubt
  • 13,897
  • 15
  • 64
  • 96

2 Answers2

12

You need to add a Web.config / App.config key for a binding redirect like so (please change the versions to what you actually need):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="ServerInterface.NET" publicKeyToken="151ae431f239ddf0" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

The oldVersion attribute sets the range of versions to redirect. The newVersion attribute sets the exact version they should redirect to.

If you're using NuGet you can do this automatically through Add-BindingRedirect. Here's an article explaining it

See here for more information on binding redirects in general.

Luke Merrett
  • 5,724
  • 8
  • 38
  • 70
  • 2
    But I do not know the exact version I want to which I want to redirect. The current version is 1.61.0.0, but some clients might have 1.62.0.0 whereas others might have 1.63.0.0 but older ones might still use the 1.61.0.0 deployed with my app. – flipdoubt Jul 06 '15 at 22:10
  • Looking at the [docs](https://msdn.microsoft.com/en-us/library/eftw1fys(v=vs.110).aspx) it appears you have to be explicit in the assembly you load with `newVersion` More details [here](http://stackoverflow.com/questions/11801219/specificversion-false-with-tfs-api-dlls). Could you transform the config file based on the client? – Luke Merrett Jul 06 '15 at 22:14
2

Redirecting the binding in code allows me to use any version. You would probably want to do more checking than this, as this redirects any failed attempts to any assembly with the same name.

public static void Main()
{
    AppDomain.CurrentDomain.AssemblyResolve += _HandleAssemblyResolve;
}

private Assembly _HandleAssemblyResolve(object sender, ResolveEventArgs args)
{
    var firstOrDefault = args.Name.Split(',').FirstOrDefault();
    return Assembly.Load(firstOrDefault);
}
Community
  • 1
  • 1
flipdoubt
  • 13,897
  • 15
  • 64
  • 96
  • You could use this: var assemblyFromTxt = new AssemblyName(args.Name); return Assembly.Load(assemblyFromTxt.Name); – squadwuschel Oct 23 '17 at 08:23
  • This is dangerous. If you can't load an assembly, it will create a stack-overflow. It can also cause autofac to stop producing error logging because it can't read the neccesary resources. It can probably cause more issues even. – Wouter Schut Oct 17 '18 at 14:51