Thread Safety and MEF Catalog

I use MEF (the one in the .Net Framework, not the System.Composition Nuget) to make DI in asp.net. I encounter memory leaks, like many of them earlier due to the fact that the MEF root NonShared IDisposable (e.g. link )

I am considering using child containers to fix this, having

  • Containers for Children Create (NonShared) Parts Specific to a Single Request
  • Parent container (specified as an export provider) containing shared parts

So something like:

CompositionContainer GetRequestContainer(CompositionContainer parent, ComposablePartCatalog catalog)
{
    return new CompositionContainer(catalog, parent);
}

Initially, I created a directory of shared and not shared parts: . To create a child container with only NonShared parts, I used FilteredCatalogs to pin the CompositionContainer parent directory so that the filtered catalog contains only NonShared parts.

This allowed a memory leak, since I could have a Dispose () child container at the end of the request, which would then release all of my NonShared IDisposable objects. Fine.

However, calling the childContainer.GetExportValues ​​function returns duplicates because there are parts from the parent container directory, as well as the child container directory.


Now I want to create 2 directories explicitly :

  • Global catalog for all shared parts
  • Local directory for all NonShared partitions

, IDisposable , , :

, ?

, N WebRequest N + 1 CompositionContainer ( + ), .

CompositionContainer parent = new CompositionContainer(Static.Global, CompositionOptions.IsThreadSafe);

CompositionContainer GetRequestContainer(CompositionContainer parent)
{
    return new CompositionContainer(Static.Local, parent);
}
+4
1

CompositionContainer (, , , CompositionOptions.IsThreadSafe ).

, , , "" , , , . .

+1

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


All Articles