举个例子,假设我正在编写一个库 CommonLib
和一个网络应用程序 WebUI
。 CommonLib
引用 NLog v1.0
和 WebUI
引用 NLog v1.1
。
我很久以前写过 CommonLib
,现在我在 WebUI
中使用它,它现在有比 CommonLib 更新的
。 NLog
版本WebUI
调用 CommonLib
来设置其日志记录。
这种情况有可能吗?我是否总是需要更新 CommonLib
中的 NLog
版本,即使方法签名在版本 1.0
和 1.1 之间没有改变
?有解决方法吗?
最佳答案
Can NLog v2 be used with Common.Logging声称您可以简单地将旧版本重定向到新版本(对于 v1 到 v2)。将此包含在您的配置文件中,匹配您的确切版本号:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="NLog" publicKeyToken="5120e14c03d0593c" culture="neutral" />
<bindingRedirect oldVersion="1.0.0.0" newVersion="1.1.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
bindingRedirect
基本上是说,“你可以继续假装新版本可以代替旧版本”。
如果那行不通,还有更多选择:
您可能需要将两个项目的 NLog 特定版本设置为 True(在每个项目 > 引用 > NLog(右键单击,选择)属性中)。使用指定的版本。
由于最新的 NLog 将包含在您的 WebUI
项目构建中,因此更困难的是 CommonLib
使用的旧 NLog。 How the Runtime Locates Assemblies对于 .Net 为尝试查找您的程序集所做的各种事情是一个很好的引用。
如果可以,请将其添加到 GAC是一个很好的方法来做到这一点。在 Web 环境中,这可能不是一种很好的方法,因为向服务器的 GAC 添加内容可能并不容易。
How to build/deploy project that requires multiple versions of the same assembly?有更多关于如何在网络环境中执行此操作的详细信息,特别是使用 web.config 的,例如你的可能是这样的:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="NLog" publicKeyToken="5120e14c03d0593c" />
<codeBase version="1.0.0.505" href="bin\NLog\v1\NLog.dll" />
<codeBase version="2.1.0.0" href="bin\NLog\v2\NLog.dll" />
</dependentAssembly>
</assemblyBinding>
</runtime>
另一种方法是使用 Assembly.LoadFrom
。例如。它适用于 WebUI 上的以下构建后事件:
copy "$(SolutionDir)packages\NLog.1.0.0.505\lib\NLog.dll" "$(TargetDir)NLogv1.dll"
在尝试使用 NLog 之前,在 CommonLib
中调用了以下行:
System.Reflection.Assembly.LoadFrom("NLogv1.dll");
关于c# - 程序集引用链是否需要传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20548558/