为什么我可以在 web.config 中有多个相同的绑定重定向?

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

假设我有一个 web.config 包含以下两个相同的部分:

<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>

为什么这样做,如果有人更改其中一个重定向(例如 newVersion="4.0.0.0" 而不是 newVersion="6.0.0.0" 会发生什么情况?

如果您为一个程序集定义了多个绑定重定向,它将使用找到的第一个并忽略所有其他重定向。

因此,如果您将第一个的 newVersion 更改为 4.0.0.0,运行时将尝试加载版本 4.0.0.0 的程序集。第二个重定向被忽略。

另见 How the Runtime Locates Assemblies。当我正确理解它时,它采用了具有匹配 assemblyIdentity

的第一个元素

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

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