Sign in to follow this  

MEF and releasing imports.

Recommended Posts

I am attempting to use MEF to load content in a program, the point is that the program is extendable so that additional modules can be easily added. However I am having trouble with MEF returning the same object repeatedly, I am not using Import attributes and am instead using the manual import methods.


I am using a custom export attribute for the classes to be composed.

[AttributeUsage(AttributeTargets.Class), PartCreationPolicy(CreationPolicy.NonShared)]
public sealed class ItemExportAttribute : ExportAttribute
	public ItemExportAttribute(string name)
		: base(name, typeof(Item)) {}

Even though I am using NonShared the same export keeps being provided, I am also composing subparts that change between calling GetExportedValue, so every object should be different. However even stepping through the debugger the constructor is only called once. Also without the NonShared policy an exception is thrown when it attempts to import an object with the same contract twice.


As far as I can tell this is mostly caused by not calling the ReleaseExport method on the CompositonContainer, however that seems rather intensive and fairly roundabout to the point where I get a feeling I'm wrong on what these parts do.

Edit: Turns out that does not fix the problem, it still only outputs one object.


Does anyone know a way to get imports without requiring every export to then be released again directly after it is imported?

Edited by Earthmark

Share this post

Link to post
Share on other sites
It's been a while since I worked with MEF but I think the problem is that you put the PartCreationPolicy in your custom attribute, it should be directly in the exported class, base class or interface.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this