加载同一程序集的多个版本
我正在使用第三方程序集,不幸的是我现在需要将他们的最新版本和以前的版本加载到我的项目中,以便在运行时我可以决定加载哪个版本。我只需要其中之一,而不是两者都需要。
考虑到这一点,我还依赖于组件提供的类型,因此我无法每次都从反射加载并查询我想要使用的方法/事件/接口。我看到有人提到通过 AppDomain 处理此问题,但我不确定如何继续。
该过程是否会针对组件的一个版本进行编码,然后在运行时(使用 AppDomain)交换到我想要使用的正确 DLL 中?那么我只会在启动时处理这个问题?
I'm working with a third-party assembly and unfortunately I now need to load their latest and a previous version into my project so at runtime I can decide which one to load. I only ever need one, not both.
With this in mind, I am also dependent upon the types provided by the components so I cannot load from reflection and query every time for the method/events/interfaces that I want to use. I have seen some mention of handling this via AppDomain
s but am not sure how to proceed.
Would the process be to code against one version of the component and then at run-time (using the AppDomain
) swap in the correct DLL I want to be consumed? So I would only be handling this at startup?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果两个程序集兼容,您可以在 app.exe.config 或 web.config 文件中定义,通过声明 bindingRedirect 始终使用新版本。
例如,
dotnet 1.0 的此配置条目告诉汇编加载程序始终使用版本 1.0.3300.0,无论调用的 exe 中编译了什么。较新的 dotnet 版本也可以实现同样的效果
If both assemblies are compatible you can define in the app.exe.config or web.config file to always use the new version by declaring bindingRedirect .
example
this config entry for dotnet 1.0 tells the asembly loader always to use version 1.0.3300.0 no matter what is compiled into the calling exe. The same is possible with newer dotnet versions
这里有几篇关于 SO 的文章,描述了如何加载同一程序集的多个版本:
这篇文章描述了如何引用 log4net 的两个不同版本。有关他如何解决问题的更多详细信息,请参阅@Joe B. 在已接受答案下的评论。
第 3 方库引用不同版本的log4net.dll
该答案引用了此链接:
在同一文件夹中使用同一程序集的不同版本
在此线程中,需要注意在同一上下文中加载同一程序集的不同版本,并引用 MSDN 上的此链接:
http://msdn.microsoft.com/en-us/library/dd153782.aspx#avoid_loading_multiple_versions
这是另一个建议使用 AssemblyResolve 的答案:
引用两个相等的程序集,仅公钥不同
Here are a couple posts from here on SO that describe how to load multiple versions of the same assembly:
This post describes how to reference two different versions of log4net. See @Joe B.'s comment under the accepted answer for a bit more detail on exactly how he solved his problem.
3rd party libraries refer to different versions of log4net.dll
That answer refers to this link:
Using different versions of the same assembly in the same folder
Within this thread, there is a caution about loading different versions of the same assembly in the same context and references this link on MSDN:
http://msdn.microsoft.com/en-us/library/dd153782.aspx#avoid_loading_multiple_versions
Here is another with an answer that suggests using AssemblyResolve:
Reference two equal assemblies, only public keys differ
如果同一组件有两个不同版本,那么这意味着两个版本不仅在实现上不同,而且在它们公开的操作数量上也不同?如果是这种情况,如果某些功能仅在两个版本之一中,您如何切换版本?
无论如何,如果您想加载两个版本,则必须在两个单独的应用程序域中执行此操作,因为这样您就可以随后卸载不需要的版本。然后,您可以在正确的应用程序域中执行逻辑,如 如何创建应用程序域并在其中运行我的应用程序?。
If you have two different versions of the same component, than this means that both versions can differ not only in implementation but also in the number of operations they expose? If this is the case, how can you switch versions if some functionality is only in one of two versions?
Anyway, if you want to load the two versions you would have to do it in two separate application domains, because then you can unload the one you don't need afterwards. Then you could execute your logic in the correct application domain, as described in How do I create an application domain and run my application in it?.