Clearing the MEF catalog

Topics: Bootstrappers & IoC
Dec 22, 2011 at 5:51 PM


i need to knoy whether is possible to clean (or Dispose) the MEF catalog elements frm Caliburn Micro, i mean, when i start the application up, i send some classes to MEF´s catalog by [Export] attribute, once i do it, every time i need that object, i just need to get it back from catalog using [Import] attribute. The issue is that i´m wandering to implement a "LogOut" behavior which should to clean the catalog and restart the application (something which is achieved pressing "F5" key). Why do not i just press the F5 key so? Because i´m developing my app to run at Silverlight, In and Out of browser, In browser every thing fine, but Out of browser it doesn´t works. Then it´so important that i clean that catalog. i already tryed some things such as folow...

1º - new ExportLifetimeContext<IMyClass>(IoC.Get<IMyClass>(), () => { GC.SuppressFinalize(f); }).Dispose();
2º - var icms = IoC.Get<IMyClass>();
3º - CompositionContainer container = CompositionHost.Initialize(new AggregateCatalog(AssemblySource.Instance.Select(x => new
       container.Compose(new CompositionBatch()); 

4º - Objs. In myClass i implemented the IDisposable interface like this:
       public void Dispose()

no one worked.

Resuming, can i clean the MEF Catalog and reset all my environment when i just click on LogOut button? If not, what would be the best approach to achieve that behavior?



Feb 12, 2013 at 12:18 PM
I too ran into the issue of needing my "shared" / "singleton" instances cleared. MEF doesn't allow you to create a new container and dispose of the old one. Once composed, you have to live with it.

Turns out MEF is not an IoC container per se, but it can offer some IoC capabilities. It has a larger focus beyond IoC - more like a run time dynamic add-in framework.

I ended up switching to AutoFac as a proper IoC and it works very well with Silverlight and VM. It can even create nested container scopes, etc. I'm sure you could consider other IoC implementations too as most of them will allow you to do exactly that.