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

此生再无相见时 提交于 2019-12-10 14:58:38

问题


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'

回答1:


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.




回答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);
}


来源:https://stackoverflow.com/questions/31255666/how-do-i-redirect-assembly-binding-to-the-current-version-or-higher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!