MEF and releasing imports.

This topic is 1815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 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 on other sites

That seems to have done it. Looks as though having that attribute is now mandatory, oh well.

Thank you Leandro.

1. 1
2. 2
Rutin
19
3. 3
JoeJ
16
4. 4
5. 5

• 36
• 23
• 13
• 13
• 17
• Forum Statistics

• Total Topics
631703
• Total Posts
3001814
×