【问题标题】:Loading DLLs into a separate AppDomain with known only common interface将 DLL 加载到具有已知唯一公共接口的单独 AppDomain 中
【发布时间】:2013-05-03 15:38:38
【问题描述】:

我需要在另一个域中加载 .dll(插件)。在主应用程序中,我对插件类型一无所知,只知道它们使用一些方法实现了公共接口 ICommonInterface。所以这段代码无济于事,因为我无法创建具有接口类型的实例。

AppDomain domain = AppDomain.CreateDomain("New domain name");
//Do other things to the domain like set the security policy

string pathToDll = @"C:\myDll.dll"; //Full path to dll you want to load
Type t = typeof(TypeIWantToLoad);
TypeIWantToLoad myObject = (TypeIWantToLoad)domain.CreateInstanceFromAndUnwrap(pathToDll, t.FullName);

我的问题是,如果我只知道实现我想要创建的类型的接口名称,我如何在新域中加载程序集并获取实例。

更新: 这是我的代码: MainLib.dll

namespace MainLib
{
public interface ICommonInterface
{
    void ShowDllName();
}
}

PluginWithOutException.dll

namespace PluginWithOutException
{
public class WithOutException : MarshalByRefObject, ICommonInterface
{
    public void ShowDllName()
    {
        Console.WriteLine("PluginWithOutException");
    }
}
}

PluginWithException.dll

namespace PluginWithException
{
public class WithException : MarshalByRefObject, ICommonInterface
{
    public void ShowDllName()
    {
        Console.WriteLine("WithException");
        throw new NotImplementedException();
    }
}
}

及主要应用:

        static void Main(string[] args)
    {
        string path = @"E:\Plugins\";
        string[] assemblies = Directory.GetFiles(path);

        List<string> plugins = SearchPlugins(assemblies);

        foreach (string item in plugins)
        {
            CreateDomainAndLoadAssebly(item);
        }

        Console.ReadKey();
    }

    public static List<string> SearchPlugins(string[] names)
    {
        AppDomain domain = AppDomain.CreateDomain("tmpDomain");
        domain.Load(Assembly.LoadFrom(@"E:\Plugins\MainLib.dll").FullName);
        List<string> plugins = new List<string>();

        foreach (string asm in names)
        {
            Assembly loadedAssembly = domain.Load(Assembly.LoadFrom(asm).FullName);

            var theClassTypes = from t in loadedAssembly.GetTypes()
                                where t.IsClass &&
                                      (t.GetInterface("ICommonInterface") != null)
                                select t;
            if (theClassTypes.Count() > 0)
            {
                plugins.Add(asm);
            }
        }
        AppDomain.Unload(domain);
        return plugins;
    }

插件和主应用程序引用了 MainLib.dll。主要目的是不在默认域中加载程序集,而是将它们加载到另一个域,所以当我不需要它们时,我只需 Unload() 域并从应用程序中卸载所有插件。

现在例外是字符串Assembly loadedAssembly = domain.Load(Assembly.LoadFrom(asm).FullName);上的FileNotFoundException, Could not load file or assembly 'PluginWithException, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.)(我试图加载名为PluginWithException的插件),我已经删除了插件中的所有依赖项,例如系统,我在这个域中加载了System.dll(它加载正确并且它在域中),但仍然无法将插件加载到域中。我还检查了,PluginWithException 有 2 个依赖项 - mscorlib 和 MainLib,它们都加载到了这个域。

更新:Here我问了这个问题的更多细节。

【问题讨论】:

    标签: c# plugins .net-assembly appdomain


    【解决方案1】:

    我不确定这是否是您需要的,我会尽力帮助您。 这就是我加载插件程序集的方式。我使用一个辅助类来管理新的 AppDomain 和该程序集上的类的实例。这是助手类:

    [Serializable, ClassInterface(ClassInterfaceType.AutoDual)]
    class helperDomain<T>: MarshalByRefObject where T: class
    {
        #region private
        private AppDomain _app_domain;
        private AppDomainSetup _app_domain_info;
    
        private string _assembly_class_name;
        private string _assembly_file;
        private string _assembly_file_name;
        private T _inner_class;
        private bool _load_ok;
        private string _loading_errors;
        private string _path;
        #endregion
    
        #region .ctor
        public helperDomain(string AssemblyFile, 
           string configFile = null, string domainName)
        {
            this._load_ok = false;
            try
            {
                this._assembly_file = AssemblyFile; //full path to assembly
                this._assembly_file_name = System.IO.Path.GetFileName(this._assembly_file); //assmbly file name
                this._path = System.IO.Path.GetDirectoryName(this._assembly_file); //get root directory from assembly path 
                this._assembly_class_name = typeof(T).ToString(); //the class name to instantiate in the domain from the assembly
                //start to configure domain
                this._app_domain_info = new AppDomainSetup();
                this._app_domain_info.ApplicationBase = this._path;
                this._app_domain_info.PrivateBinPath = this._path;
                this._app_domain_info.PrivateBinPathProbe = this._path;
                if (!string.IsNullOrEmpty(configFile))
                {
                    this._app_domain_info.ConfigurationFile = configFile;
                }
                //lets create the domain
                this._app_domain = AppDomain.CreateDomain(domainName, null, this._app_domain_info);
                //instantiate the class
                this._inner_class = (T) this._app_domain.CreateInstanceFromAndUnwrap(this._assembly_file, this._assembly_class_name);
                this._load_ok = true;
            }
            catch (Exception exception)
            {
                //There was a problema setting up the new appDomain
                this._load_ok = false;
                this._loading_errors = exception.ToString();
            }
        }
        #endregion
    
    
        #region public properties
        public string AssemblyFile
        {
            get
            {
                return _assembly_file;
            }
        }
    
        public string AssemblyFileName
        {
            get
            {
                return _assembly_file_name;
            }
        }
    
        public AppDomain AtomicAppDomain
        {
            get
            {
                return _app_domain;
            }
        }
    
        public T InstancedObject
        {
            get
            {
                return _inner_class;
            }
        }
    
        public string LoadingErrors
        {
            get
            {
                return _loading_errors;
            }
        }
    
        public bool LoadOK
        {
            get
            {
                return _load_ok;
            }
        }
    
        public string Path
        {
            get
            {
                return _path;
            }
        }
        #endregion
    }
    

    然后加载插件(每个在不同的文件夹中)。

    foreach(string pluginassemblypath in pluginspaths)
    { 
        //Each pluginassemblypath (as it says..) is the full path to the assembly
        helperDomain<IPluginClass> isoDomain = 
            helperDomain<IPluginClass>(pluginassemblypath, 
                 pluginassemblypath + ".config", 
                 System.IO.Path.GetFileName(pluginassemblypath) + ".domain");
        if (isoDomain.LoadOK)
        {
           //We can access instance of the class (.InstancedObject)
           Console.WriteLine("Plugin loaded..." + isoDomain.InstancedObject.GetType().Name);
        }
        else
        {
           //Something happened...
           Console.WriteLine("There was en error loading plugin " + 
                pluginassemblypath + " - " + helperDomain.LoadingErrors);
        }
    }
    

    希望对你有帮助...

    【讨论】:

    • 您将获得T、this._assembly_class_name = typeof(T).ToString(); 的名称,然后创建它的一个实例this._app_domain.CreateInstanceFromAndUnwrap(this._assembly_file, this._assembly_class_name);。并且 T 被硬编码到泛型类型参数中。正如用户所说,他不知道 T 是什么,只知道 T 实现了一个通用接口。
    【解决方案2】:

    这个问题似乎与您想做什么有关。

    How to Load an Assembly to AppDomain with all references recursively?

    加载程序集后,您可以使用Assembly.GetTypes() 并迭代查找实现您的接口的类型。

    【讨论】:

    • 好的,但是这个程序集将加载到默认域中,并且会一直存在,直到我关闭应用程序。我想在另一个域中加载程序集,并在需要时将其与所有程序集一起卸载。
    • 问题是我不能使用代码Assembly a = anotherDomain.Load(Assembly.LoadFrom(path).FullName);,它会抛出fileNotFountException,但是这个路径中存在.dll
    • 公平积分。这并不像看起来那么简单。我修改了我的答案。
    • 我已经尝试了这些解决方案,但它没有帮助(同样的问题,系统找不到指定的文件。
    • 从阅读另一个线程,听起来你需要自己加载依赖项。这可能是你遇到的问题。但是,您的问题的答案是您必须搜索程序集中的所有类型。他们中的许多人可能会实现您的界面。您的其他问题似乎是一个单独的问题。
    猜你喜欢
    • 2010-09-10
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    相关资源
    最近更新 更多