Lifetime of parts created by MEF container

You might be confused that when lifetime of parts that are created by MEF container.GetExportedValues appeared to be NOT controlled by their container. the confusion comes from that fact that most of MEF documentations have not mentioned that parts could be referenced by other objects in your system apart from container, in that case, container will not be responsible for life time of parts SOLELY.

1. Parts created by container will be referenced by Container, if no other references are made to these parts, it will be freed when container is freed.

If other references are made to these parts apart from container, even container is freed, parts will live on as there are other references to these parts

2. If these parts are shared, [PartCreationPolicy(CreationPolicy.Shared)] or not any annotation (default to shared) , the instance only instantiated once even it is exported more than once by same container, note : same container, it only guarantees shared between one container. So if you have different container, it still can be instantiated twice.

I hope this helps you to understand the confusing explanations out there when you want to understand the lifetime of parts

Tags:

This entry was posted on Wednesday, October 3rd, 2012 at 12:00 am and is filed under ASP.NET. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

*