Configuring .NET Framework Assembly-binding Redirects

  Subscribe
2/4/2020 - Marco von Ballmoos (updated on 9/18/2020)

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

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

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.

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.

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.

And here it is in text/code form:

<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>
Marklic
3/10/2020

Kamagra Gi http://apcialisle.com/# - cialis without prescription Viagra Online Kaufen Zollfrei <a href=http://apcialisle.com/#>Buy Cialis</a> Discount Where To Purchase Generic Direct Levaquin Canada In Internet

Anidh
5/1/2020

Thanks a lot.. It saved ton of time !!

cheapest cialis 20mg
8/11/2020

alquiler kamagra Kneetersek https://acialisse.com/ - daily cialis online Sossinoinexy levitra rendeles Pydroriarica <a href=https://acialisse.com/#>Cialis</a> asperb Buy Cheap Cialis Discount Online

cialis without prescription
8/18/2020

Brand Cialis And Levitra immumb https://biracialism.com/ - Cialis tummimberwet Solodyn Cephalexin Interaction Problems piepsyjearee <a href=https://biracialism.com/#>Cialis</a> innoky Cat Amoxicillin

where to buy cialis
9/13/2020

Viagra Alcool Tabac immumb https://cialisse.com/ - buy cialis online no prescription tummimberwet Lilly Cymbalta piepsyjearee <a href=https://cialisse.com/#>generic 5mg cialis best price</a> innoky Tadalafil Generic Tijuana Mexico

Sign up for our Newsletter