This project has moved. For the latest updates, please go here.

Doubt about CreationPolicy.NonShared

Mar 13, 2012 at 3:11 PM


When I create a class like this

[Export, PartCreationPolicy(CreationPolicy.NonShared)]
public class Foo

and I instantiate it using

Foo f;

// or

/*CompositionContainer*/ container.GetExportedValue<Foo>();

Does the container store a reference of the created instance?

Mar 13, 2012 at 4:20 PM


If Foo implements IDisposable, or has recomposable imports, then yes - the container will track a reference to the instance.

When creating many instances of a non-shared part, it is advisable to employ either nested containers or ExportFactory<T> (with or without CompositionScopeDefinition) to ensure that the instances can be released when appropriate.

Hope this helps,

Mar 13, 2012 at 8:32 PM

Hi Nick,

Thank you for your quick response.

I'm creating a desktop application so I can't use ExportFactory<T>. But now I've another doubt if the container isn't tracking the references of non-shared parts how I can released them using a nested container?

Mar 13, 2012 at 8:43 PM

You're welcome. When you get exports from a nested container, the lifetime of any tracked parts will be bound to the nested container. Disposing (or GC'ing) the nested container then releases the tracked parts.


Mar 13, 2012 at 8:47 PM

I see.

Thank you once again for your help


Mar 14, 2012 at 9:51 AM


You can look at MEF2 bits which does have ExportFactory<T> on the desktop side.