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

Jim*_*Aho 2 c# asp.net .net-assembly

假设我有一个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>
Run Code Online (Sandbox Code Playgroud)

为什么这会起作用,如果有人更改重定向之一,例如改为newVersion="4.0.0.0"而不是 ,会发生什么newVersion="6.0.0.0"

Jeh*_*hof 5

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

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

另请参阅运行时如何定位程序集。当我正确理解它时,它需要第一个匹配的元素assemblyIdentity

元素是顺序敏感的.....在重定向冲突的情况下,使用配置文件中第一个匹配的重定向语句。

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