Declaring MEF Parts to be Transient
I had very hard time locating the following information and, therefore, would like to share it with others thru my blog:
MEF allows your to declare your exports as either singleton or transient. The default policy is to create all parts (aka exports) as a singleton. What this means is that the MEF container will search inside the specified catalogs and providers to find your export. Once it does, it will create an instance of it and reuse it forever. That’s good if your exports aren’t storing any state. But if they are, then you’ll have to override the default policy by making your exports transient. You can do so by adding the below attribute to your class:
[PartCreationPolicy(CreationPolicy.NonShared)] [Export(typeof(MyType), "MyRuleset")] public class MyExportClass { }
Hope this saves you the time that it took me to find this little nugget!
Happy Coding!
|
|
|