Unloading a DLL file in mef

I have plugins as DLL files. My application loads the dll and it works fine. but when I try to remove the old plugin and replace it with a new plugin, this does not allow me. since it was downloaded by the application. I found that with appdomain we can do this. but I can not find a solution that uses mef.

I need code that can work on mef. Below is the code used to download the plugins.

//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 ie the Import and Export components assembles here container.ComposeParts(this); 
+6
source share
2 answers

I found that using appdomain we can do this. but I can not find a solution that uses mef.

Unfortunately, this is not supported by MEF. MEF was designed specifically for application extensibility, and not as a general-purpose paid system that supports offloading and code replacement at runtime.

The only way to make this work is to use MEF in a separate AppDomain and unload the entire AppDomain. The CLR itself does not support unloading a loaded assembly, except for unloading the entire AppDomain in which the assembly opens.

+4
source

If you try to insert a single Assembly object into the directory, for example:

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

You can delete / modify the file later ...

+1
source

Source: https://habr.com/ru/post/909606/


All Articles