2

我正在尝试创建一个插件框架并通过 C# 中的反射加载依赖项。我目前的实现如下:

public interface IPlugin : IDisposable
{
    void Run();
}

public interface IPluginProxy : IDisposable
{
    void RunProxy();
    void LoadDependencies(string pluginDirectoryPath, AppDomain pluginDomain);
}

public class PluginProxy : MarshalByRefObject, IPluginProxy, IDisposable
    {
        private List<IPlugin> pluginTypes = null;


        public void ProcessProxy()
        {
            //loop thorough plugin types and call Run on all plugins
            foreach(var pluginType in pluginTypes)
            {
                pluginType.Run();
            }
        }

        public void LoadDependencies(string pluginDirectoryPath, AppDomain pluginDomain)
        {
            pluginTypes = Utility.LoadAssembliesAndGetPluginTypes(pluginDirectoryPath, pluginDomain);            
        }

        //TODO:
        public void Dispose(){}

    }

public Class Utility
{
    public static List<IPlugin> LoadAssembliesAndGetPluginTypes(string pluginDirectoryPath, AppDomain pluginDomain)
    {
        try
        {
            var pluginTypes = new List<IPlugin>();
            var pluginsDirectory = new DirectoryInfo(pluginDirectoryPath);
            var files = pluginsDirectory.GetFiles("*.dll", SearchOption.TopDirectoryOnly);

            foreach (FileInfo file in files)
            {
                // Load the assembly into the child application domain.
                Assembly assembly = pluginDomain.Load(AssemblyName.GetAssemblyName(file.FullName));
                var iPluginTypes = zGetTypes(assembly);
                pluginTypes.AddRange(iPluginTypes.ToList());
            }

            return pluginTypes;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private static IEnumerable<IPlugin> zGetTypes(Assembly assembly)
    {
        var myPlugins = from y in assembly.GetTypes()
                             where y.IsClass && y.GetInterface("IPlugin") != null &&
                                   y.GetConstructor(Type.EmptyTypes) != null
                             select Activator.CreateInstance(y) as IPlugin;
        return myPlugins;
    }
}

我有一个创建新应用程序域的 Windows 服务。然后它获取 PluginProxy 并调用 LoadDependencies 和 ProcessProxy。请注意,我正在尝试将依赖项加载到子域中。

问题在于 zGetTypes 方法。该方法能够找到 IPlugin 类型(智能感知显示它)。但是,即使调用 Activator.CreateInstance,类型也不会被初始化(null)。

请注意,如果我在子域中,zGetTypes 无法创建 IPlugin 类型的实例。

程序集程序集 = pluginDomain.Load(AssemblyName.GetAssemblyName(file.FullName));

如果我不创建单独的 appdomain 并且只是将程序集加载到主 appdomain 中,则可以创建实例。在我的例子中,Service 的 appdomain 创建了子 appdomain。我想将加载插件程序集的责任推给代理。代理从 MarshalByRefObject 派生,因此我可以创建一个实例并从 Windows 服务端解包它。

然后,该服务将插件的加载和生命周期管理委托给代理。代理将通过 LoadDependencies 的方法参数获取子 appdomain 的上下文。

任何想法为什么不创建 IPlugin 的实例?

4

1 回答 1

0

I was able to resolve the issue by inferring domain at the proxy level. In my windows service, i was creating instance of plugin proxy following way:

IPluginProxy pluginProxy = IPluginProxy)
(pluginDomain
.CreateInstanceFromAndUnwrap(directory.FullName + "\\Utility.Common.dll", 
typeof(PluginProxy).FullName));

In my call to LoadAssemblies, I was passing the child appdomain as parameter: LoadDependencies(string pluginDirectoryPath, AppDomain pluginDomain);

This was unnecessary since proxy was within the context of child domain. This solved the issue.

于 2015-02-16T16:35:09.447 回答