Using multiple versions of a DLL in ASP.NET and .NET apps

Reason

Dependency version clashes are common when working with Sitecore, or any other large 3rd party API for that matter.

The following post describes how you can include multiple versions of the same assembly in your application, e.g. a Sitecore website.

For an in-depth reference, see the official Microsoft documentation on the codeBase configuration element and the related assembly binding configuration section.

Code & Example

One of the more common clashes I’ve run into when working with Sitecore is the popular Newtonsoft.Json framework — there are simply a ton of web related NuGet-packages which depend on it, because it’s that good!

The code shown below uses the codeBase configuration element to specify the location of multiple versions of Newtonsoft.Json included in our app.

In our case the additional configuration is added to the Web.config-file, and the additional assembly has been added to a subfolder of the website’s bin-folder.

<configuration>
  [...]
  <runtime>
    [...]
    <assemblybinding xmlns="urn:schemas-microsoft-com:asm.v1">  
      [...]    
      <dependentassembly>
        <!-- Sitecore default config -->
        <assemblyidentity name="Newtonsoft.Json" publickeytoken="30ad4fe6b2a6aeed" />
        <bindingredirect oldversion="0.0.0.0-6.0.0.0" newversion="6.0.0.0" />
        <!-- Additional config pointing out the location of 
             the various versions included in our solution -->
        <codebase version="6.0.0.0" href="bin\Newtonsoft.Json.dll" />
        <codebase version="7.0.0.0" href="bin\Newtonsoft v7.0\Newtonsoft.Json.dll" />
      </dependentassembly>
    </assemblybinding>
  </runtime>
</configuration>