|<<>>|6 of 210 Show listMobile Mode

Configuring .NET Framework Assembly-binding Redirects

Published by marco on

Updated by marco on

After years of getting incrementally better at fixing binding redirects, I’ve finally taken the time to document my methodology for figuring out what to put into app.config or web.config files.

The method described below works: when you get an exception because the runtime gets an unexpected version of an assembly—e.g. “The located assembly’s manifest definition does not match the assembly reference”—this technique lets you formulate a binding-redirect that will fix it. You’ll then move on to the next binding issue, until you’ve taken care of them all and your code runs again.

Automatic Binding Redirects

If you have an executable, you can usually get Visual Studio (or MSBuild) to regenerate your binding redirects for you. Just delete them all out of the app.config or web.config and Rebuild All. You should see a warning appear that you can double-click to generate binding redirects.

If, however, this doesn’t work, then you’re on your own for discovering which version you actually have in your application. You need to know the version or you can’t write the redirect. You can’t just take any number: it has to match exactly.

Testing Assemblies

Where the automatic generation of binding redirects doesn’t work is for unit-test assemblies.

My most recent experience was when I upgraded Quino-Windows to use the latest Quino-Standard. The Quino-Windows test assemblies were suddenly no longer able to load the PostgreSql driver. The Quino.Data.PostgreSql assembly targets .NET Standard 2.0. The testing assemblies in Quino-Windows target .NET Framework.

After the latest upgrade, many tests failed with the following error message:

Could not load file or assembly ‘System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

This is the version that it was looking for. It will either be the version required by the loading assembly (npgsql in this case) or the version already specified in the app.config (that is almost certainly out of date).

Which File Was Loaded?

To find out the file version that your application actually uses, you have to figure out which assembly .NET loaded. A good first place to look is in the output folder for your executable assembly (the testing assembly in this case).

If, for whatever reason, you can’t find the assembly in the output folder—or it’s not clear which file is being loaded—you can tease the information out of the exception itself.

  1. From the exception settings, make sure that the debugger will stop on a System.IO.FileLoadException
  2. Debug your test
  3. The debugger should break on the exception

 Assembly-binding exception

Click “View Details” to show the QuickWatch window for the exception. There’s a property called FusionLog that contains more information.

 Fusion log details (with loaded assembly path)

The log is quite detailed and shows you the configuration file that was used to calculate the redirect as well as the file that it loaded.

 Assembly-binding 'fusion log'

Which Version Is It?

With the path to the assembly in hand, it’s time to get the assembly version.

Showing the file properties will most likely not show you the assembly version. For third-party assemblies (e.g. Quino), the file version is often the same as the assembly version (for pre-release versions, it’s not). However, Microsoft loves to use a different file version than the assembly version. That means that you have to open the assembly in a tool that can dig that version out of the assembly manifest.

The easiest way to get the version number is to use the free tool JetBrains DotPeek or use the AssemblyExplorer in JetBrains ReSharper or JetBrains Rider.

You can see the three assemblies that I had to track down in the following screenshot.

 Actual versions of various System assemblies

Writing Binding Redirects

Armed with the actual versions and the public key-tokens, I was ready to create the app.config file for my testing assembly.

 Assembly-binding mappings

And here it is in text/code form:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity 
          name="System.Numerics.Vectors"
          publicKeyToken="B03F5F7F11D50A3A"
          culture="neutral"
        />
        <bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity
          name="System.Runtime.CompilerServices.Unsafe"
          publicKeyToken="B03F5F7F11D50A3A"
          culture="neutral"
        />
        <bindingRedirect oldVersion="0.0.0.0-4.0.5.0" newVersion="4.0.5.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity
          name="System.Threading.Tasks.Extensions"
          publicKeyToken="CC7B13FFCD2DDD51"
          culture="neutral
        "/>
        <bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>