如何在MEF中处理具有相同依赖项的不同版本的模块?

Pro*_*ofK 9 .net c# wpf mef composition

目前,我已经配置了一个模块文件夹,并且所有模块程序集及其依赖项都存在于那里.我担心,在半年的时间里,有人建立一个新的模块,和它的依赖覆盖旧版本的依赖.

如果我想自己开发某种模块的注册表,如果开发者注册了一个新的模块,并赋予它的模块文件夹的子文件夹的名字?DirectoryCatalog如果我必须告诉主机模块,这种方式会减少使用a的便利性.

sky*_*dev 4

我过去也遇到过类似的问题。下面我提出我的解决方案,我认为它与您想要实现的目标类似。

像这样使用 MEF 确实令人着迷,但这里是我的警告:

  • 事情很快就会变得复杂
  • 您必须做出一些妥协,例如继承MarshalByRefObject和插件不使用解决方案构建
  • 而且,正如我所决定的,越简单越好!其他非 MEF 设计可能是更好的选择。

好吧,免责声明就这样……

.NET 允许您将同一程序集的多个版本加载到内存中,但不能卸载它们。这就是为什么我的方法需要一个 AppDomain 来允许您在新版本可用时卸载模块。

下面的解决方案允许您在运行时将插件 dll 复制到 bin 目录中的“plugins”文件夹中。随着新插件的添加和旧插件的覆盖,旧插件将被卸载并加载新插件,而无需重新启动应用程序。如果目录中同时存在多个不同版本的 dll,您可能需要修改以PluginHost通过文件的属性读取程序集版本并采取相应的操作。

共有三个项目:

  • ConsoleApplication.dll(仅引用 Integration.dll)
  • 集成.dll
  • TestPlugin.dll(引用Integration.dll,必须复制到ConsoleApplication bin/Debug/plugins)

控制台应用程序.dll

class Program
{
    static void Main(string[] args)
    {
        var pluginHost = new PluginHost();
        //Console.WriteLine("\r\nProgram:\r\n" + string.Join("\r\n", AppDomain.CurrentDomain.GetAssemblies().Select(a => a.GetName().Name)));
        pluginHost.CallEach<ITestPlugin>(testPlugin => testPlugin.DoSomething());
        //Console.ReadLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

集成.dll

PluginHost 允许您与插件进行通信。PluginHost 应该只有一个实例。这也起到了轮询的作用DirectoryCatalog

public class PluginHost
{
    public const string PluginRelativePath = @"plugins";
    private static readonly object SyncRoot = new object();
    private readonly string _pluginDirectory;
    private const string PluginDomainName = "Plugins";
    private readonly Dictionary<string, DateTime> _pluginModifiedDateDictionary = new Dictionary<string, DateTime>();
    private PluginDomain _domain;

    public PluginHost()
    {
        _pluginDirectory = AppDomain.CurrentDomain.BaseDirectory + PluginRelativePath;
        CreatePluginDomain(PluginDomainName, _pluginDirectory);
        Task.Factory.StartNew(() => CheckForPluginUpdatesForever(PluginDomainName, _pluginDirectory));
    }

    private void CreatePluginDomain(string pluginDomainName, string pluginDirectory)
    {
        _domain = new PluginDomain(pluginDomainName, pluginDirectory);
        var files = GetPluginFiles(pluginDirectory);
        _pluginModifiedDateDictionary.Clear();
        foreach (var file in files)
        {
            _pluginModifiedDateDictionary[file] = File.GetLastWriteTime(file);
        }
    }
    public void CallEach<T>(Action<T> call) where T : IPlugin
    {
        lock (SyncRoot)
        {
            var plugins = _domain.Resolve<IEnumerable<T>>();
            if (plugins == null)
                return;
            foreach (var plugin in plugins)
            {
                call(plugin);
            }
        }
    }

    private void CheckForPluginUpdatesForever(string pluginDomainName, string pluginDirectory)
    {
        TryCheckForPluginUpdates(pluginDomainName, pluginDirectory);
        Task.Delay(5000).ContinueWith(task => CheckForPluginUpdatesForever(pluginDomainName, pluginDirectory));
    }

    private void TryCheckForPluginUpdates(string pluginDomainName, string pluginDirectory)
    {
        try
        {
            CheckForPluginUpdates(pluginDomainName, pluginDirectory);
        }
        catch (Exception ex)
        {
            throw new Exception("Failed to check for plugin updates.", ex);
        }
    }

    private void CheckForPluginUpdates(string pluginDomainName, string pluginDirectory)
    {
        var arePluginsUpdated = ArePluginsUpdated(pluginDirectory);
        if (arePluginsUpdated)
            RecreatePluginDomain(pluginDomainName, pluginDirectory);
    }

    private bool ArePluginsUpdated(string pluginDirectory)
    {
        var files = GetPluginFiles(pluginDirectory);
        if (IsFileCountChanged(files))
            return true;
        return AreModifiedDatesChanged(files);
    }

    private static List<string> GetPluginFiles(string pluginDirectory)
    {
        if (!Directory.Exists(pluginDirectory))
            return new List<string>();
        return Directory.GetFiles(pluginDirectory, "*.dll").ToList();
    }

    private bool IsFileCountChanged(List<string> files)
    {
        return files.Count > _pluginModifiedDateDictionary.Count || files.Count < _pluginModifiedDateDictionary.Count;
    }

    private bool AreModifiedDatesChanged(List<string> files)
    {
        return files.Any(IsModifiedDateChanged);
    }

    private bool IsModifiedDateChanged(string file)
    {
        DateTime oldModifiedDate;
        if (!_pluginModifiedDateDictionary.TryGetValue(file, out oldModifiedDate))
            return true;
        var newModifiedDate = File.GetLastWriteTime(file);
        return oldModifiedDate != newModifiedDate;
    }

    private void RecreatePluginDomain(string pluginDomainName, string pluginDirectory)
    {
        lock (SyncRoot)
        {
            DestroyPluginDomain();
            CreatePluginDomain(pluginDomainName, pluginDirectory);
        }
    }

    private void DestroyPluginDomain()
    {
        if (_domain != null)
            _domain.Dispose();
    }
}
Run Code Online (Sandbox Code Playgroud)

Autofac 是此代码的必需依赖项。PluginDomainDependencyResolver 在插件 AppDomain 中实例化。

[Serializable]
internal class PluginDomainDependencyResolver : MarshalByRefObject
{
    private readonly IContainer _container;
    private readonly List<string> _typesThatFailedToResolve = new List<string>();

    public PluginDomainDependencyResolver()
    {
        _container = BuildContainer();
    }

    public T Resolve<T>() where T : class
    {
        var typeName = typeof(T).FullName;
        var resolveWillFail = _typesThatFailedToResolve.Contains(typeName);
        if (resolveWillFail)
            return null;
        var instance = ResolveIfExists<T>();
        if (instance != null)
            return instance;
        _typesThatFailedToResolve.Add(typeName);
        return null;
    }

    private T ResolveIfExists<T>() where T : class
    {
        T instance;
        _container.TryResolve(out instance);
        return instance;
    }

    private static IContainer BuildContainer()
    {
        var builder = new ContainerBuilder();

        var assemblies = LoadAssemblies();
        builder.RegisterAssemblyModules(assemblies); // Should we allow plugins to load dependencies in the Autofac container?
        builder.RegisterAssemblyTypes(assemblies)
            .Where(t => typeof(ITestPlugin).IsAssignableFrom(t))
            .As<ITestPlugin>()
            .SingleInstance();

        return builder.Build();
    }

    private static Assembly[] LoadAssemblies()
    {
        var path = AppDomain.CurrentDomain.BaseDirectory + PluginHost.PluginRelativePath;
        if (!Directory.Exists(path))
            return new Assembly[]{};
        var dlls = Directory.GetFiles(path, "*.dll").ToList();
        dlls = GetAllDllsThatAreNotAlreadyLoaded(dlls);
        var assemblies = dlls.Select(LoadAssembly).ToArray();
        return assemblies;
    }

    private static List<string> GetAllDllsThatAreNotAlreadyLoaded(List<string> dlls)
    {
        var alreadyLoadedDllNames = GetAppDomainLoadedAssemblyNames();
        return dlls.Where(dll => !IsAlreadyLoaded(alreadyLoadedDllNames, dll)).ToList();
    }

    private static List<string> GetAppDomainLoadedAssemblyNames()
    {
        var assemblies = AppDomain.CurrentDomain.GetAssemblies();
        return assemblies.Select(a => a.GetName().Name).ToList();
    }

    private static bool IsAlreadyLoaded(List<string> alreadyLoadedDllNames, string file)
    {
        var fileInfo = new FileInfo(file);
        var name = fileInfo.Name.Replace(fileInfo.Extension, string.Empty);
        return alreadyLoadedDllNames.Any(dll => dll == name);
    }

    private static Assembly LoadAssembly(string path)
    {
        return Assembly.Load(File.ReadAllBytes(path));
    }
}
Run Code Online (Sandbox Code Playgroud)

此类代表实际的插件应用程序域。解析到此域的程序集应首先从 bin/plugins 文件夹加载所需的任何依赖项,然后加载 bin 文件夹,因为它是父 AppDomain 的一部分。

internal class PluginDomain : IDisposable
{
    private readonly string _name;
    private readonly string _pluginDllPath;
    private readonly AppDomain _domain;
    private readonly PluginDomainDependencyResolver _container;

    public PluginDomain(string name, string pluginDllPath)
    {
        _name = name;
        _pluginDllPath = pluginDllPath;
        _domain = CreateAppDomain();
        _container = CreateInstance<PluginDomainDependencyResolver>();
    }

    public AppDomain CreateAppDomain()
    {
        var domaininfo = new AppDomainSetup
        {
            PrivateBinPath = _pluginDllPath
        };
        var evidence = AppDomain.CurrentDomain.Evidence;
        return AppDomain.CreateDomain(_name, evidence, domaininfo);
    }

    private T CreateInstance<T>()
    {
        var assemblyName = typeof(T).Assembly.GetName().Name + ".dll";
        var typeName = typeof(T).FullName;
        if (typeName == null)
            throw new Exception(string.Format("Type {0} had a null name.", typeof(T).FullName));
        return (T)_domain.CreateInstanceFromAndUnwrap(assemblyName, typeName);
    }

    public T Resolve<T>() where T : class
    {
        return _container.Resolve<T>();
    }

    public void Dispose()
    {
        DestroyAppDomain();
    }

    private void DestroyAppDomain()
    {
        AppDomain.Unload(_domain);
    }
}
Run Code Online (Sandbox Code Playgroud)

最后是你的插件接口。

public interface IPlugin
{
    // Marker Interface
}
Run Code Online (Sandbox Code Playgroud)

主应用程序需要了解每个插件,因此需要一个接口。它们必须继承并在方法IPlugin中注册PluginHost BuildContainer

public interface ITestPlugin : IPlugin
{
    void DoSomething();
}
Run Code Online (Sandbox Code Playgroud)

测试插件.dll

[Serializable]
public class TestPlugin : MarshalByRefObject, ITestPlugin
{
    public void DoSomething()
    {
        //Console.WriteLine("\r\nTestPlugin:\r\n" + string.Join("\r\n", AppDomain.CurrentDomain.GetAssemblies().Select(a => a.GetName().Name)));
    }
}
Run Code Online (Sandbox Code Playgroud)

最后的想法...

该解决方案对我有用的原因之一是来自 AppDomain 的插件实例的生命周期非常短。但是,我相信可以进行修改以支持具有更长生命周期的插件对象。这可能需要一些妥协,例如更高级的插件包装器,它可能会在重新加载 AppDomain 时重新创建对象(请参阅 参考资料CallEach)。