Does the MEF Container.Dispose dispose added catalogs?

According to the MEF Preview 9 source code (which probably closely matches the code that shipped in . NET 4) CompositionContainer will wrap the catalog in a CatalogExportProvider This export provider is stored in a field and disposed along with the container. However CatalogExportProvider.

Dispose will not in turn dispose the wrapped ComposablePartCatalog Therefore the answer is no: CompositionContainer does not dispose the catalog You can verify this by running this code, which will not print anything to the console: class MyCatalog : ComposablePartCatalog { protected override void Dispose(bool disposing) { Console. WriteLine("Disposed! "); base.Dispose(); } public override IQueryable Parts { get { throw new NotImplementedException(); } } } class Program { static void Main(string args) { var container = new CompositionContainer(new MyCatalog()); container.Dispose(); Console.ReadKey(); } }.

According to the MEF Preview 9 source code (which probably closely matches the code that shipped in . NET 4) CompositionContainer will wrap the catalog in a CatalogExportProvider. This export provider is stored in a field and disposed along with the container.

However, CatalogExportProvider. Dispose will not in turn dispose the wrapped ComposablePartCatalog. Therefore the answer is no: CompositionContainer does not dispose the catalog.

You can verify this by running this code, which will not print anything to the console: class MyCatalog : ComposablePartCatalog { protected override void Dispose(bool disposing) { Console. WriteLine("Disposed! "); base.Dispose(); } public override IQueryable Parts { get { throw new NotImplementedException(); } } } class Program { static void Main(string args) { var container = new CompositionContainer(new MyCatalog()); container.Dispose(); Console.ReadKey(); } }.

Ah! The create custom subclass technique :) My reflector copy ran out and it was a Friday so posted it on SO. Thanks Wim!

– Gishu Apr 16 at 14:45.

As Wim already discovered neither the CompsitionContainer nor the CatalogExportProvider will call dispose on the catalog. Neither of them created the catalog thus neither of them own it and as such will not call Dispose on it. Whoever constructs the catalog should be the one that disposes it.

There are a lot of scenarios where someone would want to share a catalog instance with multiple containers which is why the container doesn't have ownership of the catlaog and therefore doesn't dispose it.

All the docs do not highlight this - the examples are usually of the form var container = new CompositionContainer(new SomeCatalog()). Is it because it's okay to not dispose the Catalog - doesn't have significant resource hangups? – Gishu Apr 21 at 2:47 The default catalogs only have locks that they potentially need to dispose but they will eventually be cleaned up.

Generally the samples are making the assumption that you have one container for the application and once you are done with it your application shuts down and at which point any OS resources would be cleaned up anyway. – Wes Haggard Apr 21 at 3:12.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions