allBlogsList

Externalizing Assembly Redirects

> Recently, a co-worker pinged me asking about the best way to handle transforms in a Sitecore 9+ PaaS environment. My response: "Transforms are the devil, and there are often zero reasons why you need them in a new Sitecore environment." Most developers are already aware of the mechanism which can be used to externalize App Settings and Connection Strings. A similar process can be used for assembly binding redirects, although comparatively it is a little quirky.


Recently, a co-worker pinged me asking about the best way to handle transforms in a Sitecore 9+ PaaS environment. My response: "Transforms are the devil, and there are often zero reasons why you need them in a new Sitecore environment."

Sitecore, of course, gives us rules-based configurations out of the box. These give us a lot of control over applying different configurations for different roles and environments, but what about modifications to the web.config? In my experience, most changes to the web.config are "one-time" changes that you can make (and document) as soon as an environment is provisioned. This includes managing assembly binding redirects, which is often something that changes each time you add a new NuGet reference to your project. Most developers are already aware of the mechanism which can be used to externalize App Settings and Connection Strings. A similar process can be used for assembly binding redirects, although comparatively it is a little quirky. 

First thing, we need to modify the web.config to know that its redirects are in a separate file. The below screenshot shows a before and after of the web.config with this modification.

Web Config Before and After

For the most part, this is congruent with a normal config externalization. The key difference is that the original parent node ( <runtime> ) is excluded from the configuration. Additionally, we are using a <linkedConfiguration> node, which points to an absolute file path reference in the site directory.

<table><tbody><tr><td><p>1</p><p>2</p><p>3</p></td><td><div><p><code><</code><code>assemblybinding</code> <code>xmlns</code><code>=</code><code>"urn:schemas-microsoft-com:asm.v1"</code><code>></code></p><p><code>  </code><code><</code><code>linkedConfiguration</code> <code>href</code><code>=</code><code>"<a href="file:///D:">file://D:</a>\home\site\wwwroot\App_Config\RuntimeSettings.config"</code> <code>/></code></p><p><code></</code><code>assemblybinding</code><code>></code></p></div></td></tr></tbody></table>

The external file itself also look a little different than what might be expected:

<table><tbody><tr><td><p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p><p>7</p><p>8</p><p>9</p><p>10</p><p>11</p><p>12</p><p>13</p><p>14</p><p>15</p><p>16</p><p>17</p><p>18</p><p>19</p></td><td><div><p><code><?</code><code>xml</code> <code>version</code><code>=</code><code>"1.0"</code><code>?></code></p><p><code><</code><code>configuration</code><code>></code></p><p><code>  </code><code><</code><code>runtime</code><code>></code></p><p><code>    </code><code><</code><code>assemblyBinding</code> <code>xmlns</code><code>=</code><code>"urn:schemas-microsoft-com:asm.v1"</code><code>></code></p><p><code>      </code><code><</code><code>dependentAssembly</code><code>></code></p><p><code>        </code><code><</code><code>assemblyIdentity</code> <code>name</code><code>=</code><code>"System.Web.Helpers"</code> <code>publicKeyToken</code><code>=</code><code>"31bf3856ad364e35"</code><code>/></code></p><p><code>        </code><code><</code><code>bindingRedirect</code> <code>oldVersion</code><code>=</code><code>"1.0.0.0-3.0.0.0"</code> <code>newVersion</code><code>=</code><code>"3.0.0.0"</code><code>/></code></p><p><code>      </code><code></</code><code>dependentAssembly</code><code>></code></p><p><code>      </code><code><</code><code>dependentAssembly</code><code>></code></p><p><code>        </code><code><</code><code>assemblyIdentity</code> <code>name</code><code>=</code><code>"System.Web.WebPages"</code> <code>publicKeyToken</code><code>=</code><code>"31bf3856ad364e35"</code><code>/></code></p><p><code>        </code><code><</code><code>bindingRedirect</code> <code>oldVersion</code><code>=</code><code>"1.0.0.0-3.0.0.0"</code> <code>newVersion</code><code>=</code><code>"3.0.0.0"</code><code>/></code></p><p><code>      </code><code></</code><code>dependentAssembly</code><code>></code></p><p><code>      </code><code><</code><code>dependentAssembly</code><code>></code></p><p><code>        </code><code><</code><code>assemblyIdentity</code> <code>name</code><code>=</code><code>"System.Web.Mvc"</code> <code>publicKeyToken</code><code>=</code><code>"31bf3856ad364e35"</code><code>/></code></p><p><code>        </code><code><</code><code>bindingRedirect</code> <code>oldVersion</code><code>=</code><code>"1.0.0.0-5.2.3.0"</code> <code>newVersion</code><code>=</code><code>"5.2.3.0"</code><code>/></code></p><p><code>      </code><code></</code><code>dependentAssembly</code><code>></code></p><p><code>    </code><code></</code><code>assemblyBinding</code><code>></code></p><p><code>  </code><code></</code><code>runTime</code><code>></code></p><p><code></</code><code>configuration</code><code>></code></p></div></td></tr></tbody></table>

In the external file, we have re-added the <runtime> node and replicated the normal assembly binding config structure thereafter. This particular file can now be added and maintained in source control, so that you can ensure that all redirects move through the environments with their correlated changes. More details on the <linkedConfiguration> node can be found here. You can note the base web.config configuration was pulled from here. The tricky part was figuring out the proper file structure for the external file, as well as the file path needed to get to that external file.

Last but not least is documentation to ensure that it is known that these steps need to be taken when provisioning a new environment, both local and non-local. I like to include a blurb in the ReadMe for my source control repository:

Documentation

I am definitely not a fan of transforms, and this is just one of the little things that makes them that much less required in a project. It is a pretty straight-forward and simple change, but can eliminate a fair amount of headache when having to manage redirects across multiple branches and environments, which are all dependent on the packages currently installed at the time.

Happy Coding!