程序集绑定重定向:如何以及为什么?
Assembly Binding redirect: How and Why?
这不是问题,而是关于程序集绑定重定向工作的一般理解问题。
查询
- 为什么绑定重定向只显示主要版本而不显示次要版本号、内部版本号和修订版号?
新旧版本只有大版本有变化才会变化吗?
<dependentAssembly>
<assemblyIdentity name="FooBar"
publicKeyToken="32ab4ba45e0a69a1"
culture="en-us" />
<bindingRedirect oldVersion="7.0.0.0" newVersion="8.0.0.0" />
</dependentAssembly>
为什么需要绑定重定向?假设您有引用库 B 的应用程序 A,以及版本 1.1.2.5 的库 C。库 B 反过来也引用库 C,但版本为 1.1.1.0。现在我们发生了冲突,因为您不能在运行时加载同一程序集的不同版本。要解决此冲突,您可以使用绑定重定向,通常指向新版本(但也可以指向旧版本)。您可以通过将以下内容添加到应用程序 A 的 app.config 文件的 configuration > runtime > assemblyBinding
部分下(有关完整配置文件的示例,请参阅 here):
<dependentAssembly>
<assemblyIdentity name="C"
publicKeyToken="32ab4ba45e0a69a1"
culture="en-us" />
<bindingRedirect oldVersion="1.1.1.0" newVersion="1.1.2.5" />
</dependentAssembly>
您还可以指定要映射的版本范围:
<bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.2.5" />
现在参考1.1.1.0版本的C编译的库B在运行时将使用1.1.2.5版本的C。当然,你最好确保库 C 是向后兼容的,否则这可能会导致意想不到的结果。
您可以重定向任何版本的库,而不仅仅是主要版本。
我们遇到了 NewtonSoft.Json 的绑定重定向问题。我们在 win 10 文件属性“9.0.1.19813”中查找文件版本,查找数字,重定向一直失败。进一步调查发现我们正在查看文件版本而不是程序集版本。所以,我想知道人们是否误解了文件版本(经常更改)和程序集版本(您在 windows 10 文件资源管理器中看不到)。要查看 dll 的汇编版本,您可以在 powershell 中 运行 这个。 将 dll 名称替换为您要为其查找版本的名称。
[Reflection.AssemblyName]::GetAssemblyName('C:\development\bin\Newtonsoft.Json.dll').Version
以上结果为
Major Minor Build Revision
----- ----- ----- --------
9 0 0 0
参见参考资料:
How can i see the assembly version of a .NET assembly in Windows Vista and newer (WIndows 7, 2008)?
https://support.microsoft.com/en-nz/help/556041
这不是问题,而是关于程序集绑定重定向工作的一般理解问题。
查询
- 为什么绑定重定向只显示主要版本而不显示次要版本号、内部版本号和修订版号?
新旧版本只有大版本有变化才会变化吗?
<dependentAssembly> <assemblyIdentity name="FooBar" publicKeyToken="32ab4ba45e0a69a1" culture="en-us" /> <bindingRedirect oldVersion="7.0.0.0" newVersion="8.0.0.0" /> </dependentAssembly>
为什么需要绑定重定向?假设您有引用库 B 的应用程序 A,以及版本 1.1.2.5 的库 C。库 B 反过来也引用库 C,但版本为 1.1.1.0。现在我们发生了冲突,因为您不能在运行时加载同一程序集的不同版本。要解决此冲突,您可以使用绑定重定向,通常指向新版本(但也可以指向旧版本)。您可以通过将以下内容添加到应用程序 A 的 app.config 文件的 configuration > runtime > assemblyBinding
部分下(有关完整配置文件的示例,请参阅 here):
<dependentAssembly>
<assemblyIdentity name="C"
publicKeyToken="32ab4ba45e0a69a1"
culture="en-us" />
<bindingRedirect oldVersion="1.1.1.0" newVersion="1.1.2.5" />
</dependentAssembly>
您还可以指定要映射的版本范围:
<bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.2.5" />
现在参考1.1.1.0版本的C编译的库B在运行时将使用1.1.2.5版本的C。当然,你最好确保库 C 是向后兼容的,否则这可能会导致意想不到的结果。
您可以重定向任何版本的库,而不仅仅是主要版本。
我们遇到了 NewtonSoft.Json 的绑定重定向问题。我们在 win 10 文件属性“9.0.1.19813”中查找文件版本,查找数字,重定向一直失败。进一步调查发现我们正在查看文件版本而不是程序集版本。所以,我想知道人们是否误解了文件版本(经常更改)和程序集版本(您在 windows 10 文件资源管理器中看不到)。要查看 dll 的汇编版本,您可以在 powershell 中 运行 这个。 将 dll 名称替换为您要为其查找版本的名称。
[Reflection.AssemblyName]::GetAssemblyName('C:\development\bin\Newtonsoft.Json.dll').Version
以上结果为
Major Minor Build Revision
----- ----- ----- --------
9 0 0 0
参见参考资料:
How can i see the assembly version of a .NET assembly in Windows Vista and newer (WIndows 7, 2008)?
https://support.microsoft.com/en-nz/help/556041