4

我已经在 MEF Codeplex 论坛上问过这个问题,但我还没有得到回复,所以我想我会尝试 StackOverflow。如果有人感兴趣,这是原始帖子(这只是它的副本):

MEF Codeplex

“首先让我说,我对 MEF 完全陌生(今天才发现它)并且到目前为止对它非常满意。但是,我遇到了一个非常令人沮丧的问题。我正在创建一个应用程序将有一个插件架构,插件将仅存储在单个 DLL 文件中(或编码到主应用程序中)。DLL 文件需要能够在运行时重新编译,应用程序应该识别并重新加载插件(我知道这很困难,但这是一个要求)。为了实现这一点,我采用了涵盖http://blog.maartenballiauw.be/category/MEF.aspx的方法那里(寻找 WebServerDirectoryCatalog)。基本上这个想法是“监视插件文件夹,将新的/修改的程序集复制到 Web 应用程序的 /bin 文件夹并指示 MEF 从那里加载其导出。” 这是我的代码,这可能不是正确的方法,但这是我在网络上的一些示例中发现的:

        main()...
    string myExecName = Assembly.GetExecutingAssembly().Location;
        string myPath = System.IO.Path.GetDirectoryName(myExecName);
        catalog = new AggregateCatalog();
        pluginCatalog = new MyDirectoryCatalog(myPath + @"/Plugins");
        catalog.Catalogs.Add(pluginCatalog);


        exportContainer = new CompositionContainer(catalog);

        CompositionBatch compBatch = new CompositionBatch();
        compBatch.AddPart(this);
        compBatch.AddPart(catalog);
        exportContainer.Compose(compBatch);

    private FileSystemWatcher fileSystemWatcher;
    public DirectoryCatalog directoryCatalog;
    private string path;
    private string extension;

    public MyDirectoryCatalog(string path)
    {
        Initialize(path, "*.dll", "*.dll");
    }

    private void Initialize(string path, string extension, string modulePattern)
    {
        this.path = path;
        this.extension = extension;
        fileSystemWatcher = new FileSystemWatcher(path, modulePattern);
        fileSystemWatcher.Changed += new FileSystemEventHandler(fileSystemWatcher_Changed);
        fileSystemWatcher.Created += new FileSystemEventHandler(fileSystemWatcher_Created);
        fileSystemWatcher.Deleted += new FileSystemEventHandler(fileSystemWatcher_Deleted);
        fileSystemWatcher.Renamed += new RenamedEventHandler(fileSystemWatcher_Renamed);
        fileSystemWatcher.IncludeSubdirectories = false;
        fileSystemWatcher.EnableRaisingEvents = true;
        Refresh();
    }
    void fileSystemWatcher_Renamed(object sender, RenamedEventArgs e)
    {
        RemoveFromBin(e.OldName);
        Refresh();
    }
    void fileSystemWatcher_Deleted(object sender, FileSystemEventArgs e)
    {
        RemoveFromBin(e.Name);
        Refresh();
    }
    void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
    {
        Refresh();
    }
    void fileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
    {
        Refresh();
    }
    private void Refresh()
    {
        // Determine /bin path 
        string binPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins");
        string newPath = "";
        // Copy files to /bin 
        foreach (string file in Directory.GetFiles(path, extension, SearchOption.TopDirectoryOnly))
        {
            try
            {
                DirectoryInfo dInfo = new DirectoryInfo(binPath);
                DirectoryInfo[] dirs = dInfo.GetDirectories();
                int count = dirs.Count() + 1;
                newPath = binPath + "/" + count;
                DirectoryInfo dInfo2 = new DirectoryInfo(newPath);
                if (!dInfo2.Exists)
                    dInfo2.Create();

                File.Copy(file, System.IO.Path.Combine(newPath, System.IO.Path.GetFileName(file)), true);
            }
            catch
            {
                // Not that big deal... Blog readers will probably kill me for this bit of code :-) 
            }
        }
        // Create new directory catalog 
        directoryCatalog = new DirectoryCatalog(newPath, extension);
        directoryCatalog.Refresh();
    }
    public override IQueryable<ComposablePartDefinition> Parts
    {
        get { return directoryCatalog.Parts; }
    }
    private void RemoveFromBin(string name)
    {
        string binPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "");
        File.Delete(Path.Combine(binPath, name));
    }

所以这一切实际上是有效的,在 main 中的代码结束后,我的 IEnumerable 变量实际上填充了 DLL 中的所有插件(如果你按照代码位于 Plugins/1 中,这样我就可以修改插件文件夹)。所以现在我应该能够重新编译插件 DLL,将其放入 Plugins 文件夹,我的 FileWatcher 检测到它已更改,然后将其复制到文件夹“2”中,directoryCatalog 应该指向新文件夹。所有这些实际上都有效!问题是,即使看起来每件事都指向了正确的位置,我的 IEnumerable 变量永远不会使用新插件进行更新。如此接近,却又如此遥远!有什么建议么?我知道这样做的缺点,实际上没有 dll 被卸载并导致内存泄漏,但它 一个 Windows 应用程序,并且可能每天至少启动一次,并且插件不太可能经常更改,但客户端仍然要求它在不重新加载应用程序的情况下执行此操作。谢谢!

感谢大家提供的任何帮助,无法弄清楚这一点让我发疯了。”

4

3 回答 3

3

没有重新组合的触发器,因为您的目录实现不提供通知。实施INotifyComposablePartCatalogChanged来解决这个问题。

于 2010-04-12T22:29:49.490 回答
1

我相信 MEF 只能加载同一个程序集的一个版本(虽然我正在尝试使用 Silverlight)

于 2010-04-13T08:22:38.157 回答
1

我遇到了类似的问题-将发现的插件复制到应用程序的目录后,即使在 DirectoryCatalog 上调用 .refresh() 后,DirectoryCatalog 也看不到它们。

我发现单步执行代码解决了这个问题——我最好的猜测是文件系统在 FileSystemWatcher 启动它的通知之后仍然需要片刻,MEF 才能扫描新程序集(可能完成一些模糊的复制操作)并查看里面的部分.

System.Threading.Thread.Sleep(1000),虽然很蹩脚,但解决了这个问题。

于 2011-08-11T18:45:57.413 回答