Search code examples
c#asp.net.net-assembly

Why can I have multiple identical binding redirects in web.config?


Assuming I have one web.config with the following two identical sections:

<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4FE6B2A6AEED" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>


<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4FE6B2A6AEED" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>

Why does this work, and what will happen if someone changes one of the redirects, such as to newVersion="4.0.0.0" instead of newVersion="6.0.0.0"?


Solution

  • If you have more than one binding redirect defined for one assembly, it uses the first one that it found and ignores all others.

    So if you change the newVersion of the first one to 4.0.0.0 the runtime will try to load Version 4.0.0.0 of the assembly. The second redirect is ignored.

    See also How the Runtime Locates Assemblies. When I understand it correctly it tooks the first element that has the matching assemblyIdentity

    The elements are order-sensitive.....In case of a conflict in redirection, the first matching redirection statement in the configuration file is used.

    Taken from https://msdn.microsoft.com/en-us/library/433ysdt1(v=vs.100).aspx