1

i have a wpf application with login window before displaying the mainwindow.

i use mef to load all modules/parts. before the mainwindow start i check the user login data against the parts which i display then. the parts a Shared and NonShared.

[ImportMany]
private IEnumerable<Lazy<IComponent, IComponentMetadata>> _components;

[ImportMany("Resourcen", typeof(ResourceDictionary))]
private IEnumerable<ResourceDictionary> _importResourcen;

var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
catalog.Catalogs.Add(new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory));
_mefcontainer = new CompositionContainer(catalog);

_mefcontainer.ComposeParts(somepartwithaSharedExport, this);

this all works fine. but now i tried the "relogin".

 _mefcontainer.Dispose();
 _mefcontainer = null;

 //here the stuff that works from above

first i thought it works, but it seems that the parts i create the first time still exist in memory and i have no chance to "kill" them. so i got OutOfMemory Exception when i relogin enough times.

that why i use this approach now

  System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);
  App.ShutDown();

i dont feel happy with this.

is there a way to cleanup the Compositioncontainer and create a new one?

blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • From the looks of it, you might not need to dump the imports in order to cycle the login. Could you explain why you need to re-compose? – mrtig Sep 12 '13 at 15:27

2 Answers2

0

You could try to call _mefcontainer.RemovePart(somepartwithaSharedExport). More details here: http://mef.codeplex.com/wikipage?title=Parts%20Lifetime

mrtig
  • 2,217
  • 16
  • 26
0

For the non-shared part you can call CompositionContainer.ReleaseExport:

_mefcontainer.ReleaseExport(nonSharedExport);

For more info have a try the sample code from this answer.

As far as I know, the shared parts cannot be released without disposing the container. If you go with that path, then you will also have to make sure that no references to these objects are kept to allow for the GC to collect them. The documentation reference from mrtig's answer provides a lot of useful details concerning the lifetime of parts and you should probably study it along with the answer by weshaggard to a similar question. It also explains what happens to disposable parts.

Community
  • 1
  • 1
Panos Rontogiannis
  • 4,154
  • 1
  • 24
  • 29