问题
我的MEF代码在运行时无法正确更新与DirectoryCatalog相关联的文件夹中的程序集。插件在运行时成功加载,但当我更新dll并在DirectoryCatalog上调用Refresh时,程序集没有得到更新。
背景
我正在构建一个包含MEF容器的dll,并使用DirectoryCatalog查找本地插件文件夹。我目前从一个简单的WinForm中调用这个dll,该WinForm已设置为使用ShadowCopy的单独项目,以便我可以覆盖插件文件夹中的dll。我不是使用FileWatcher来更新此文件夹,而是公开了一个公共方法,在其中调用DirectoryCatalog上的refresh,因此我可以随意更新程序集,而不是自动更新。
代码
基类实例化MEF目录和容器,并将它们保存为类变量以供稍后引用。
public class FieldProcessor
{
private CompositionContainer _container;
private DirectoryCatalog dirCatalog;
public FieldProcessor()
{
var catalog = new AggregateCatalog();
//Adds all the parts found in the same assembly as the TestPlugin class
catalog.Catalogs.Add(new AssemblyCatalog(typeof(TestPlugin).Assembly));
dirCatalog = new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory + "Plugin\\");
catalog.Catalogs.Add(dirCatalog);
//Create the CompositionContainer with the parts in the catalog
_container = new CompositionContainer(catalog);
}
public void refreshCatalog()
{
dirCatalog.Refresh();
}
} ...
这是我想要覆盖的插件。我的测试方式是,将返回的字符串输出到文本框中,然后更改插件返回的字符串,重新构建,并将其复制到插件文件夹中。但在运行的应用程序中,它不会更新,直到我关闭并重新启动应用程序。
[Export(typeof(IPlugin))]
[ExportMetadata("PluginName", "TestPlugin2")]
public class TestPlugin2 : IPlugin
{
public IEnumerable<IField> GetFields(ContextObject contextObject, params string[] parameters)
{
List<IField> retList = new List<IField>();
//Do Work Return Wrok Results
retList.Add(new Field("plugin.TestPlugin2", "TestPluginReturnValue2"));
return retList;
}
}
编辑
导入语句 [ImportMany(AllowRecomposition=true)]
IEnumerable<Lazy<IPlugin, IPluginData>> plugins;