Jump to content

  • Log In with Google      Sign In   
  • Create Account

MEF and releasing imports.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Earthmark   Members   -  Reputation: 198

Like
0Likes
Like

Posted 25 July 2013 - 07:55 PM

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, 25 July 2013 - 09:10 PM.


Sponsor:

#2 Leandro GR   Members   -  Reputation: 516

Like
0Likes
Like

Posted 26 July 2013 - 07:08 AM

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.

#3 Earthmark   Members   -  Reputation: 198

Like
0Likes
Like

Posted 26 July 2013 - 07:20 PM

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

 

Thank you Leandro.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS