加载同一程序集的多个版本 [英] Loading multiple versions of the same assembly

查看:27
本文介绍了加载同一程序集的多个版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

考虑到这一点,我还依赖于组件提供的类型,因此我无法每次都从反射和查询中加载我想要使用的方法/事件/接口.我已经看到有人提到通过 AppDomain 处理这个问题,但我不确定如何继续.

该过程是否会针对组件的一个版本进行编码,然后在运行时(使用 AppDomain)交换我想要使用的正确 DLL?所以我只会在启动时处理这个?

解决方案

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

示例

<预><代码><配置><运行时><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1" applyTo="v1.0.3705"><从属程序集><assemblyIdentity name="Regcode" publicKeyToken="b03f5f7f11d50a3a"culture=""/><bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="1.0.3300.0"/></dependentAssembly></assemblyBinding></运行时></配置>

dotnet 1.0 的这个配置条目告诉汇编加载器总是使用 1.0.3300.0 版本,无论什么被编译到调用 exe 中.较新的 dotnet 版本也是如此

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?

解决方案

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

这篇关于加载同一程序集的多个版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆