【问题标题】:Late binding in child AppDomain (.Net)子 AppDomain (.Net) 中的后期绑定
【发布时间】:2010-10-01 02:41:46
【问题描述】:

我正在尝试在 .Net 中开发插件架构。该应用程序将是一个 .Net 应用程序。会有包含插件的目录。每个目录将代表一个插件。每个插件目录也将包含所有依赖 dll。插件需要存储在单独的 AppDomain 中,因为插件可能使用相同的程序集,但版本不同。

当它遍历 Init() 中的 foreach 循环时,我得到一个 System.IO.FileNotFoundException:无法加载文件或程序集“[程序集名称],版本=1.0.0.0,文化=中性,PublicKeyToken=null”或其依赖项之一。系统找不到指定的文件。 在 _apDomain.Load() 中用于不在主项目中的程序集。

代码:

    readonly private AppDomain _appDomain;
    internal ItemModuleAppDomain()
    {
        AppDomainSetup info = AppDomain.CurrentDomain.SetupInformation;
        _appDomain = AppDomain.CreateDomain("ChildDomain");
    }

    public void Init(string dllDir)
    {
        string[] dlls = Directory.GetFiles(dllDir, "*.dll", SearchOption.TopDirectoryOnly);
        foreach(string dll in dlls)
        {
            _appDomain.Load(AssemblyName.GetAssemblyName(dll));
        }

知道为什么吗?我尝试了几种方法,例如将程序集读取为要加载的字节数组。

【问题讨论】:

    标签: c# asp.net appdomain


    【解决方案1】:

    我相信AppDomain.Load 可能是您的目的的错误方法 - 它将程序集加载到当前应用程序域和目标域中。你会得到一个错误,因为你的程序集位于一个子文件夹中,而 .NET 不知道它。您必须在配置中提供私有路径 (http://msdn.microsoft.com/en-us/library/15hyw9x3.aspx)。也可以使用AppDomain的AssemblyResolveTypeResolve事件进行解析。

    您还应该查看 AppDomain.CreateInstanceFrom 方法来加载您的主要插件类型(和包含程序集)。

    【讨论】:

    • 谢谢。我们最终对 dll 进行了严格命名,以便将它们加载到同一个 AppDomain 中。
    猜你喜欢
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-29
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    相关资源
    最近更新 更多