What exactly does this line in the .csproj mean?
<Reference Include="Newtonsoft.Json, Version=126.96.36.199, 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= is an XML tag denoting an assembly reference into a project.
The text within the include tag Newtonsoft.Json, Version=188.8.131.52, 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 184.108.40.206 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 220.127.116.11. 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 v18.104.22.168 of Newtonsoft.Json while another one could rely on v22.214.171.124. 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 v126.96.36.199 of the assembly? What would the assembly which relied on 188.8.131.52 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-184.108.40.206" newVersion="220.127.116.11" /> </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 18.104.22.168, please look for an use Newtonsoft.Json version 22.214.171.124.
This will all work like magic but you better be sure Newtonsoft.Json 126.96.36.199 deployed alongside with your application and that this version is fully compatible with all versions within the range 0.0.0.0 to 188.8.131.52.