A refresher on .NET Binding Redirect

What exactly does this line in the .csproj mean?

<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
 <HintPath>..\..\packages\Newtonsoft.Json.1.0.2\lib\4.0\Newtonsoft.Json.dll</HintPath>
 <Private>True</Private>
 </Reference>

First, let break down each line at a time:

<Reference Include=

<Reference Include=   is an XML tag denoting an assembly reference into a project.

The text within the include tag        Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL    is the fully qualified name of the .NET assembly.

<HintPath>  denotes a location where Visual Studio will first attempt to look for the referenced DLL before starting to look in its probing paths.

As you can see, there are two file versions here, 1.0.2 and 4.5.0.0 which typically lead to confusion.  However, when having a discussion around assemblies, it is best to stick with the one used in the fully qualified name, which in this case is 4.5.0.0.  This is the version we use in the rest of this article.

So, why does all of this matter?

Different projects in a solution can use different versions of a DLL.  For example, one project could rely v4.5.0.0 of Newtonsoft.Json while another one could rely on v6.0.0.0. However, when both projects are built to formulate the solution package, which one will be used, if both have the correct version of these assemblies in their hint paths?

As we all know, or we should, there cannot be two DLLs with the same name within a folder.  So when our solution is deployed, there will be only one Newtonsoft.Json in the installation folder.  The version deployed, will depend on the last project that was built and its output copied into the installation folder for our application.

But, what if we deployed v4.5.0.0 of the assembly?  What would the assembly which relied on 6.0.0.0 of this assembly do, when it has to resolve its types?

 

This is where binding redirect for .NET comes in.  In simple terms binding redirect instructs the .NET runtime on what version of an assembly to use if it cannot find the one that was specified in the assembly manifest.  This is a configuration in app.config or web.config as typically looks like this:

<runtime>
<dependentAssembly>
 <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
 <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="4.5.0.0" />
 </dependentAssembly>
 </assemblyBinding>
 </runtime>

Basically, we are saying, her .NET runtime, if you are attempting to resolve an assembly called Newtonsoft.Json which has any version from 0.0.0.0 to 8.0.0.0, please look for an use Newtonsoft.Json version 4.5.0.0.

This will all work like magic but you better be sure Newtonsoft.Json 4.5.0.0 deployed alongside with your application and that this version is fully compatible with all versions within the range 0.0.0.0 to 8.0.0.0.

Happing Coding.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s