Does MEF Container.Dispose provide added directories?

It looks like my code looks

var catalog = new AssemblyCatalog(typeof(Program).Assembly); _container = new CompositionContainer(catalog); 

Code analysis shows a CA2000 warning: Dispose in a directory before all references to it go out of scope.

So I'm not sure if I need to suppress the warning or turn _catalog into a + Dispose it field.

MEF docs don't seem to mention this.

+6
source share
2 answers

According to the MEF Preview 9 source code (which probably closely matches the code that ships in .NET 4), the CompositionContainer will wrap the directory in CatalogExportProvider . This export provider is stored in the field and placed with the container. However, CatalogExportProvider.Dispose will not take turns removing the wrapped ComposablePartCatalog .

Therefore, the answer is: CompositionContainer does not delete the directory.

You can verify this by running this code that will not print anything on the console:

 class MyCatalog : ComposablePartCatalog { protected override void Dispose(bool disposing) { Console.WriteLine("Disposed!"); base.Dispose(); } public override IQueryable<ComposablePartDefinition> Parts { get { throw new NotImplementedException(); } } } class Program { static void Main(string[] args) { var container = new CompositionContainer(new MyCatalog()); container.Dispose(); Console.ReadKey(); } } 
+4
source

Since Wim has already discovered that neither CompsitionContainer nor CatalogExportProvider will be called in the directory. None of them created a directory, therefore, none of them owns it and as such will not call Dispose on it. The one who creates the directory must be the one who uses it.

There are many scenarios in which someone wants to share a directory instance with multiple containers, so the container does not have ownership of catlaog and therefore does not dispose of it.

+3
source

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


All Articles