Patch binding redirects through transform file

Binding redirect patch

How to patch binding redirects in web.config through transform files.

Binding redirects are used to redirect one assembly version to another.

This is usually done by adding or modifying entries in your application’s web.config file. However, in some cases you don’t want to change the web.config directly, but patch through transform files instead (e.g. Sitecore Habitat uses gulp and msbuild to achieve this).

The xml below applies a binding redirect to assemble Newtonsoft.Json. It contains two tranform operations. The first one ,InsertIfMissing, inserts a new entry if the assembly entry doesn’t exist, and the second one, Replace, replaces it in case the existing redirect already exist.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1">
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly xdt:Transform="InsertIfMissing" xdt:Locator="Condition(asmv1:assemblyIdentity/@name='Newtonsoft.Json')">
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" xmlns="urn:schemas-microsoft-com:asm.v1"/>
<bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" xmlns="urn:schemas-microsoft-com:asm.v1" />
</dependentAssembly>
<dependentAssembly xdt:Transform="Replace" xdt:Locator="Condition(asmv1:assemblyIdentity/@name='Newtonsoft.Json')">
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" xmlns="urn:schemas-microsoft-com:asm.v1"/>
<bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" xmlns="urn:schemas-microsoft-com:asm.v1" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

Pay attention at the namespace asmv1 added to the configuration element. It is needed for this to work.

NB: I wanted to avoid adding assemblyIdentity and bindingRedirect elements in the InsertIfMissing transform but couldn’t make it work. Let me know if you know how.


Please let me know what you think and/or if you can spot any errors.
/eom

Share