插件 AppDomains 解决方法 [英] Plugin AppDomains Workaround

查看:35
本文介绍了插件 AppDomains 解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理自己子目录中的插件程序集时,存在一个众所周知的问题,即这些程序集一旦尝试从其子目录加载各自的依赖项,就会加载失败.一个解决方案是在 AppDomains 中加载插件,这些插件在初始化时在它们的 AppDomainSetup 对象中设置了它们的 PrivateBinPath.然而,这会导致有关编组/跨 AppDomain 通信的其他困难,特别是如果插件应该提供一些 GUI.

When dealing with plugin assemblies in their own subdirectories, there is the well-known problem that these assemblies fail to load once they try to load their respective dependencies from their subdirectories. A solution is to load the plugins in AppDomains which had their PrivateBinPath set in their AppDomainSetup object upon initialization. However, this causes other difficulties concerning marshalling/cross-AppDomain communication, in particular if the plugins are supposed to provide some GUI.

当安全方面的优先级较低时(非关键实用程序应用程序,由于插件故障导致崩溃时没有严重问题),我有以下想法:在应用程序启动时,应搜索所有插件目录,并且应该创建一个新的 AppDomain,在其 bin 路径中包含这些目录.然后,整个应用程序及其 GUI 以及所有插件都在该新 AppDomain 中运行.

When security aspects have a lower priority (non-critical utility application, no severe problems upon crashes caused by faulty plugins), I've had the following idea: Upon application start-up, all plugin directories should be searched for, and a new AppDomain should be created that has those directories in its bin path. Then, the whole application and its GUI run in that new AppDomain, along with all plugins.

在给定的情况下,是否有任何理由避免该解决方案?或者可能有什么原因导致该解决方案甚至不可行?

Under the given circumstances, are there any reasons to avoid that solution? Or are there maybe any reasons why that solution isn't even feasible?

推荐答案

考虑到您所描述的场景,我不知道您提出的第二个域有任何相关问题.但是,您也可以通过在 addins 子目录中搜索自己并使用 Assembly.LoadFrom.

Taking in consideration your described scenario I don't know of any issue associated with your proposal for a second domain. However, you may also investigate the possibility of handling the assembly loading failures on the initial domain by searching yourself through the addins sub-directories and loading the assembly from there using Assembly.LoadFrom.

可能的设置示例,其中必须实现 FindAssemblyByName 以搜索所有可能的位置:

Example of a possible setup for this, where the FindAssemblyByName would have to be implemented to search through all the possible locations:

static void Main(string[] args)
{
    AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

    // ...
}

static Assembly CurrentDomain_AssemblyResolve(
    object sender, 
    ResolveEventArgs e)
{
    var assemblyName = new AssemblyName(e.Name);

    string assemblyFilePath = FindAssemblyByName(assemblyName);

    if (string.IsNullOrEmpty(assemblyFilePath))
        return null;

    return Assembly.LoadFrom(assemblyFilePath);
}

这篇关于插件 AppDomains 解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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