我的项目引用了Library1.dll和Library2.dll。 Library2.dll 依赖于 Library1.dll,但它被编译为通过不同的名称 Library1.Net40.dll 来引用它。
有没有一种好的方法告诉我的应用程序将 Library1.Net40.dll 的所有引用重定向到 Library1.dll?也许类似于使用 重定向版本的方式?
我有一个处理 AppDomain.AssemblyResolve 事件的解决方案,但这有点麻烦,我希望有更好的方法来做到这一点。
编辑:
供任何人参考,以下是我最终使用 AppDomain.AssemblyResolve 事件重定向到不同的程序集。
My project references Library1.dll and Library2.dll. Library2.dll has a dependency on Library1.dll, but it was compiled to reference it by a different name, Library1.Net40.dll.
Is there an nice way tell my application to redirect all references for Library1.Net40.dll to resolve to Library1.dll? Maybe something similar to the way you can redirect versions using a <bindingRedirect>?
I've got a solution that handles the AppDomain.AssemblyResolve event, but it's a bit of a hack and am hoping there's a better way to do this.
Edit:
For anyone's reference, here's how I ended up solving it using the AppDomain.AssemblyResolve event to redirect to a different assembly.
发布评论
评论(2)
您是否尝试过使用元素 ?
(未经测试;不知道它是否有效。)
CF:我将此更新放在这里,因为评论有点长:)
好主意,谢谢。我的重定向工作正常,但它抱怨因为名称不同,日志如下:
Have you tried playing with the <codeBase> element?
(Untested; no idea if it works.)
CF: Am putting this update here, because it's a bit long for the comments :)
Good idea, thanks. I got the redirect working but it complains because the names are different, here's the log:
应用PARTIAL解析时,程序集名称必须与文件名匹配。但是,文件的位置可能不同。
否则 Fusion 绑定日志将报告“WRN:比较程序集名称导致不匹配:NAME”并且无法绑定。
(好消息:可以重命名程序集 DLL 以匹配程序集名称。)
例如:
这将解析
bin/Newtonsoft.Json.dll
和bin /Newtonsoft.Json.12/Newtonsoft.Json.dll
,具体取决于版本(分别为 6-10 或 11-12)。即使目录路径不同,NAME 也成功匹配文件名。注意“bin”本身就是替代版本的 href 的一部分;根据应用基础进行相关调整,这与探测路径不同。对于在 IIS 下运行的 about 来说,应用程序库位于 bin 目录之上。 (请参阅 Fusion 日志中的“LOG: Appbase = ..”。)
不幸的是,无论配置文件如何,MSBuild 进程都不会自动遵循引用程序集的目录结构。将项目设置为不“复制本地”替代程序集版本,然后将它们复制为辅助流程的一部分,确保维护正确的结构。如果任何已编译的程序集将备用版本作为直接引用,那么最好确保默认情况下没有一个版本是“复制本地”。
When a PARTIAL resolve is applied, the ASSEMBLY NAME must match the filename. However the LOCATION of the file can be different.
Otherwise the Fusion Binding Log will report "WRN: Comparing the assembly name resulted in the mismatch: NAME" and be unable to bind.
(Good news: it is possible to rename the assembly DLL to match the Assembly Name.)
For example:
This resolves
bin/Newtonsoft.Json.dll
andbin/Newtonsoft.Json.12/Newtonsoft.Json.dll
, depending on version (6-10 or 11-12, respectively). The NAME successfully matches the filename even though the directory path is different.N.B. "bin" is itself part of the href to the alternate version; adjust as relevant depending on the application base, which is different than the probe path. In the case about, which is running under IIS, the app base is a level above the bin directoy. (See "LOG: Appbase = .." in the Fusion Log.)
Unfortunately the MSBuild process does not honor the directory structure of Referenced Assemblies automatically, regardless of any configuration files. Set the project to not "Copy Local" the alternative assembly versions and then copy them as part of a secondary process ensuring the correct structure is maintained. If any compiled assembly take the alternate version as a directly reference it might be good to ensure that none are "Copy Local" by default.