Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在mef中卸载dll文件_C#_Dll_Mef - Fatal编程技术网

C# 在mef中卸载dll文件

C# 在mef中卸载dll文件,c#,dll,mef,C#,Dll,Mef,我有一些插件作为dll文件。我的应用程序加载dll,运行正常。但是当我尝试删除旧插件并用新插件替换它时,它不允许我这么做。因为它已由应用程序加载。我发现通过使用appdomain我们可以做到这一点。但我无法找到使用mef的解决方案 我需要一个可以在mef上运行的代码。下面是我用来加载插件的代码 //Creating an instance of aggregate catalog. It aggregates other catalogs var aggregateCatalog = new A

我有一些插件作为dll文件。我的应用程序加载dll,运行正常。但是当我尝试删除旧插件并用新插件替换它时,它不允许我这么做。因为它已由应用程序加载。我发现通过使用appdomain我们可以做到这一点。但我无法找到使用mef的解决方案

我需要一个可以在mef上运行的代码。下面是我用来加载插件的代码

//Creating an instance of aggregate catalog. It aggregates other catalogs
var aggregateCatalog = new AggregateCatalog();

//Build the directory path where the parts will be available
var directoryPath = "Path to plugins folder";

//Load parts from the available dlls in the specified path using the directory catalog
var directoryCatalog = new DirectoryCatalog(directoryPath, "*.dll");

//Add to the aggregate catalog
aggregateCatalog.Catalogs.Add(directoryCatalog);

//Crete the composition container
var container = new CompositionContainer(aggregateCatalog);


// Composable parts are created here i.e. the Import and Export components assembles here
container.ComposeParts(this);
我发现通过使用appdomain我们可以做到这一点。但我无法找到使用mef的解决方案

不幸的是,MEF不支持这一点。MEF是专门为应用程序的可扩展性而设计的,而不是作为一个支持在运行时卸载和替换代码的通用插件系统


实现此功能的唯一方法是在单独的AppDomain中使用MEF,并将AppDomain作为一个整体卸载。CLR本身不支持卸载加载的程序集,只支持卸载打开该程序集的整个AppDomain。

如果您尝试在目录中插入一个程序集对象,如下所示:

Assembly assembly = Assembly.Load(System.IO.File.ReadAllBytes(Path.Combine(directoryPath, ItemPlugin)));
aggregateCatalog.Catalogs.Add(new AssemblyCatalog(assembly));

您可以稍后删除/更改文件…

应该有一个解决方案。我听说这将出现在mef的下一个版本中。不知道这是真是假。@fhnaseer不知道-至少到目前为止还没有公开展示过。这确实超出了MEF的预期用途范围,所以我怀疑它是否会发生。在CLR中删除程序集的唯一受支持的方法是卸载整个AppDomain。MEF作为一个图书馆,无法“解决”这个问题。那么“AllowRecomposition=true”在ImportMany中的含义是什么呢。不管我写不写,行为都是一样的。@fhnaseer允许MEF引入更多的程序集,但它从不删除它们。基本上,IEnumerable是用额外的新类型重新创建的,但旧程序集仍保留在内存中。您可以从aggregateCatalog中删除它,但如果不卸载appdomain,您将无法从appdomain卸载程序集。