8

I have some plugins as dll files. My application loads the dll and it runs fine. but when I try to delete the old plugin and replace it with a new plugin it doesn't allow to do me. as it has been loaded by application. I have found that by using appdomain we can do that. but I am not able to find a solution in which mef is used.

I need a code which can run on mef. Below is my code which is used to load 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 i.e. the Import and Export components assembles here
container.ComposeParts(this);
Jonny
  • 2,509
  • 23
  • 42
fhnaseer
  • 7,159
  • 16
  • 60
  • 112

2 Answers2

5

i have found that by using appdomain we can do that. but i am not able to find a solution in which mef is used.

Unfortunately, this is not supported by MEF. MEF was designed specifically for application extensibility, not as a general purpose plugin system which supports unloading and replacement of code at runtime.

The only way to make this work would be to use MEF within a separate AppDomain, and unload the AppDomain as a whole. The CLR itself has no support for unloading a loaded assembly other than unloading the entire AppDomain in which the assembly is opened.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • there should be a solution to this. i heard that this is going to be in mef next release. dont know it is true or not. – fhnaseer Feb 29 '12 at 07:23
  • 1
    @fhnaseer Nope - at least not in anything publicly shown so far. This is really outside of the intended use case for MEF, so I doubt it will happen there. THe only supported way to remove an assembly in the CLR is to unload the entire AppDomain. MEF, being a library, can't "work around" that. – Reed Copsey Feb 29 '12 at 07:32
  • well then what is the meaning of "AllowRecomposition = true" in ImportMany. If i write it or not behaviour is same. – fhnaseer Feb 29 '12 at 07:47
  • 1
    @fhnaseer It allows MEF to bring in more assemblies - but it never removes them. Basically, the IEnumerable is recreated with extra, *new* types, but the old assemblies stay loaded in memory. – Reed Copsey Feb 29 '12 at 08:36
1

If you try to insert on catalog one Assembly Object like this:

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

You can Delete/Change the file later...

jcvpacheco
  • 91
  • 4
  • 1
    You can delete it from aggregateCatalog but you wont be able to unload the assembly from the appdomain without unloading the hole appdomain. – Peter Oct 20 '14 at 12:49