How do I redirect an assembly binding to the current version or higher?

Even though my links have Specific Version

set to false

, I get 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 where some target computers may be 1.61.0.0 and others may be 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 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'

      

+3


source to share


2 answers


Redirecting the binding to code allows any version to be used. You probably want to do more checks than this, as this will redirect any failed attempts of any build 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);
}

      

+2


source


You need to add the Web.config / App.config key to redirect the binding (please change the versions to what you really 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 attribute oldVersion

sets the redirection range of versions. The attribute newVersion

sets the exact version they should be redirected to.



If you are using NuGet, you can do this automatically via Add-BindingRedirect

. Here's an article explaining it

See here for more information on link redirects in general.

+8


source







All Articles