c# – MEF插件未更新
作者:互联网
我已经在MEF Codeplex论坛上问了这个,但我还没有得到回复,所以我想我会尝试StackOverflow.如果有人感兴趣,这是原始帖子(这只是它的副本):
“首先我要说的是,我对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));
}
所以这一切实际上都有效,并且在主要代码结束后,我的IEnumerable变量实际上填充了DLL中的所有插件(如果您按照代码位于Plugins / 1中,那么我可以修改dll中的插件文件夹).
所以现在在这一点上,我应该能够重新编译插件DLL,将其放入Plugins文件夹,我的FileWatcher检测到它已被更改,然后将其复制到文件夹“2”中,directoryCatalog应该指向新文件夹.这些所有
实际上有效!问题是,即使看起来每个东西都指向正确的位置,我的IEnumerable变量也永远不会使用新的插件进行更新.如此接近,但到目前为止!有什么建议?
我知道这样做的缺点,没有dll实际上被卸载并导致内存泄漏,但它是一个Windows应用程序,可能每天至少启动一次,并且插件不太可能改变
通常,但客户仍然要求它在不重新加载应用程序的情况下执行此操作.谢谢!
感谢你们所能提供的任何帮助,这让我发疯,无法解决这个问题.“
解决方法:
没有重构的触发器,因为您的目录实现不提供通知.实施INotifyComposablePartCatalogChanged来解决这个问题.
标签:c,frameworks,mef,managed,extensible 来源: https://codeday.me/bug/20190621/1258669.html