加载同一程序集的多个版本

发布于 2024-10-07 22:27:54 字数 266 浏览 1 评论 0原文

我正在使用第三方程序集,不幸的是我现在需要将他们的最新版本和以前的版本加载到我的项目中,以便在运行时我可以决定加载哪个版本。我只需要其中之一,而不是两者都需要。

考虑到这一点,我还依赖于组件提供的类型,因此我无法每次都从反射加载并查询我想要使用的方法/事件/接口。我看到有人提到通过 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 AppDomains 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七颜 2024-10-14 22:27:54

如果两个程序集兼容,您可以在 app.exe.config 或 web.config 文件中定义,通过声明 bindingRedirect 始终使用新版本。

例如,

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1" appliesTo="v1.0.3705">
        <dependentAssembly>
            <assemblyIdentity name="Regcode" publicKeyToken="b03f5f7f11d50a3a" culture=""/>
            <bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="1.0.3300.0"/>
        </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

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

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1" appliesTo="v1.0.3705">
        <dependentAssembly>
            <assemblyIdentity name="Regcode" publicKeyToken="b03f5f7f11d50a3a" culture=""/>
            <bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="1.0.3300.0"/>
        </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

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

千纸鹤带着心事 2024-10-14 22:27:54

这里有几篇关于 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

往昔成烟 2024-10-14 22:27:54

如果同一组件有两个不同版本,那么这意味着两个版本不仅在实现上不同,而且在它们公开的操作数量上也不同?如果是这种情况,如果某些功能仅在两个版本之一中,您如何切换版本?

无论如何,如果您想加载两个版本,则必须在两个单独的应用程序域中执行此操作,因为这样您就可以随后卸载不需要的版本。然后,您可以在正确的应用程序域中执行逻辑,如 如何创建应用程序域并在其中运行我的应用程序?

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?.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文