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