Configuring .NET Framework Assembly-binding Redirects
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
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
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.
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-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:
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.
- From the exception settings, make sure that the debugger will stop on a
- Debug your test
- 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:
<?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-220.127.116.11" newVersion="18.104.22.168"/> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="B03F5F7F11D50A3A" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-22.214.171.124" newVersion="126.96.36.199"/> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="CC7B13FFCD2DDD51" culture="neutral "/> <bindingRedirect oldVersion="0.0.0.0-188.8.131.52" newVersion="184.108.40.206"/> </dependentAssembly> </assemblyBinding> </runtime> </configuration>