<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.microsoft.co.il/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Bnaya Eshet : Export</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx</link><description>Tags: Export</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>Immutable Collections</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/02/17/immutable-collections.aspx</link><pubDate>Sun, 17 Feb 2013 19:45:12 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1751972</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;Immutable Collections &lt;/h2&gt;  &lt;p&gt;&lt;strong&gt;Immutability&lt;/strong&gt; is a pattern which is suit well &lt;strong&gt;parallel programming&lt;/strong&gt;,     &lt;br /&gt;but you have to be aware of a potential &lt;strong&gt;memory pressure risk&lt;/strong&gt; when it&amp;#39;s not implemented right or used wisely.&lt;/p&gt;  &lt;p&gt;this post will cover a new &lt;strong&gt;BCL library&lt;/strong&gt; (still in its preview stage) which is targeting&lt;strong&gt; immutable collections&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_0382_692BD55A.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="Immutable, Parallel, Task, BCL, collection, Thread-safe" border="0" alt="Immutable, Parallel, Task, BCL, collection, Thread-safe" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_0382_thumb_6CE97A2A.jpg" width="423" height="308" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;.NET&lt;/strong&gt; is already having &lt;strong&gt;Concurrent implementation&lt;/strong&gt; for Queue, Stack, Bug and Dictionary, which is &lt;strong&gt;thread-safe&lt;/strong&gt;, but other type of collection like List is missing.     &lt;br /&gt;another type of collection are &lt;strong&gt;read only collection&lt;/strong&gt;, but those type of collection are &lt;strong&gt;not truly thread-safe&lt;/strong&gt; because it is just a wrapper upon other collection which can be mutate by it&amp;#39;s owner.     &lt;br /&gt;so &lt;strong&gt;iterating on a read only collection doesn&amp;#39;t guarantee thread-safety&lt;/strong&gt; because the collection can be mutate during the iteration by it&amp;#39;s original owner.     &lt;br /&gt;currently we do have one option to create an &lt;strong&gt;immutable collection&lt;/strong&gt; by using the &lt;strong&gt;ToArray()&lt;/strong&gt; extension method, but as I mentioned before it &lt;strong&gt;can lead to a memory pressure&lt;/strong&gt; while using a large collection.&lt;/p&gt;  &lt;p&gt;the &lt;strong&gt;BCL team&lt;/strong&gt; was targeting this issue and are working on a new kind of collections which are immutable.     &lt;br /&gt;those collection is having a cool concept which guarantee that an immutable sequence will not changed, but it can be part of another immutable sequence. this mean that you can take an immutable collection and add an item. this will keep the immutable part as is and you will get a new collection that include your addition and the original collection, but if someone is using the original collection he won&amp;#39;t be affected by your mutation.     &lt;br /&gt;as you can see the &lt;strong&gt;BCL immutable collection&lt;/strong&gt; &lt;strong&gt;can be&lt;/strong&gt; &lt;strong&gt;mutate&lt;/strong&gt; but the mutation will return a new collection and leave the original collection untouched.&lt;/p&gt;  &lt;h5&gt;Start using the immutable collection&lt;/h5&gt;  &lt;p&gt;you can start using it though &lt;a href="https://nuget.org/packages/Microsoft.Bcl.Immutable"&gt;NuGet&lt;/a&gt;.     &lt;br /&gt;on the current preview version it includes:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;ImmutableStack&amp;lt;T&amp;gt; &lt;/li&gt;    &lt;li&gt;ImmutableQueue&amp;lt;T&amp;gt; &lt;/li&gt;    &lt;li&gt;ImmutableList&amp;lt;T&amp;gt; &lt;/li&gt;    &lt;li&gt;ImmutableHashSet&amp;lt;T&amp;gt; &lt;/li&gt;    &lt;li&gt;ImmutableSortedSet&amp;lt;T&amp;gt; &lt;/li&gt;    &lt;li&gt;ImmutableDictionary&amp;lt;K, V&amp;gt; &lt;/li&gt;    &lt;li&gt;ImmutableSortedDictionary&amp;lt;K, V&amp;gt; &lt;/li&gt; &lt;/ul&gt;  &lt;h5&gt;Fluent API&lt;/h5&gt;  &lt;p&gt;I will speak about 3 APIs:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Add &lt;/li&gt;    &lt;li&gt;AddRange &lt;/li&gt;    &lt;li&gt;Builder &lt;/li&gt; &lt;/ul&gt;  &lt;h6&gt;Add&lt;/h6&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6e44ba97-9c59-49e9-8793-4d1ed33706a6" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Test&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; List_AddItem_Test()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; col = &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ImmutableList&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;.Empty;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; col1 = col.Add(1);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; col2 = col1.Add(2);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AreEqual(0, col.Count);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AreEqual(1, col1.Count);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AreEqual(2, col2.Count);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;whenever you want an &lt;strong&gt;empty instance&lt;/strong&gt; of immutable collection you should use the &lt;strong&gt;static Empty property&lt;/strong&gt; (there is no public constructor available).&lt;/p&gt;  &lt;p&gt;you should always remember that Adding an item will &lt;strong&gt;not affect the original collection&lt;/strong&gt; therefore you must &lt;strong&gt;handle the return value&lt;/strong&gt; (is is the same concept as string.Replace).&lt;/p&gt;  &lt;p&gt;finally you can notice at lines 8-10 that the above code was actually construct 3 different immutable collections.&lt;/p&gt;  &lt;h6&gt;AddRange&lt;/h6&gt;  &lt;p&gt;both from &lt;strong&gt;performance&lt;/strong&gt; and &lt;strong&gt;usability&lt;/strong&gt; it is better using the &lt;strong&gt;AddRange&lt;/strong&gt; API when you intend to add multiple items.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4a59b070-234d-40da-af88-f5149f6ae735" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Test&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; List_AddRange_Test()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; col = &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ImmutableList&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;.Empty;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; col1 = col.AddRange(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Enumerable&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Range(0, 1000));&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AreEqual(0, col.Count);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AreEqual(1000, col1.Count);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;h6&gt;Builder&lt;/h6&gt;  &lt;p&gt;if you want to &lt;strong&gt;efficiently&lt;/strong&gt; &lt;strong&gt;add multiple items&lt;/strong&gt; and AddRange doesn&amp;#39;t do the job for you, you can use a &lt;strong&gt;Builder&lt;/strong&gt; (the concept is quit similar to StringBuilder).     &lt;br /&gt;you should remember that &lt;strong&gt;unlike the immutable collection a builder is not a thread-safe&lt;/strong&gt; structure, therefore you shouldn&amp;#39;t share its state between threads.     &lt;br /&gt;when you complete the mutation, you can &lt;strong&gt;extract an immutable collection from the builder&lt;/strong&gt;.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:74e71650-2eaf-46b2-b766-eed18f1569aa" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Test&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; List_Builder_Test()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; col = &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Enumerable&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Range(0, 1000).ToImmutableList();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; builder = col.ToBuilder();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;for&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; i = 0; i &amp;lt; 100; i++)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;builder.Remove(i * 10);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; col1 = builder.ToImmutable();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AreEqual(1000, col.Count);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AreEqual(900, col1.Count);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;strong&gt;line 5&lt;/strong&gt;: getting a builder from immutable collection.     &lt;br /&gt;&lt;strong&gt;line 10&lt;/strong&gt;: getting an immutable back from the builder (after the mutation).&lt;/p&gt;  &lt;h5&gt;Migration&lt;/h5&gt;  &lt;p&gt;if you are using a read only collection though the IReadOnlyCollection, IReadOnlyList or IReadOnlyDictionary, migration will be easy because the immutable collections are implementing those interfaces.&lt;/p&gt;  &lt;h5&gt;Optimization&lt;/h5&gt;  &lt;p&gt;we already spoke about the memory pressure optimization, but what is the characterize of those collection in compare with the well known BCL mutable collection?    &lt;br /&gt;as the BCL team argue that &lt;strong&gt;immutable collections have been heavily tuned for maximum performance and minimum GC pressure&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;the following table was published by the &lt;strong&gt;BCL team&lt;/strong&gt;.     &lt;br /&gt;it &lt;strong&gt;compare the performance behavior of the BCL immutable and immutable&lt;/strong&gt; &lt;strong&gt;collection&lt;/strong&gt; (and remember that this is only a preview version which mean that further optimization can be applied).&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/immutable-table_3C72F5B4.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="Immutable, Parallel, Task, BCL, collection, Thread-safe" border="0" alt="Immutable, Parallel, Task, BCL, collection, Thread-safe" src="http://blogs.microsoft.co.il/blogs/bnaya/immutable-table_thumb_33A2ED68.png" width="492" height="179" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;you can read more on this at &lt;a href="http://blogs.msdn.com/b/bclteam/archive/2012/12/18/preview-of-immutable-collections-released-on-nuget.aspx"&gt;this&lt;/a&gt; post.&lt;/p&gt;  &lt;h5&gt;summary&lt;/h5&gt;  &lt;p&gt;&lt;strong&gt;Immutability&lt;/strong&gt; is a great pattern for &lt;strong&gt;parallelism&lt;/strong&gt;.     &lt;br /&gt;the BCL team are giving us a robust and &lt;strong&gt;well design immutable collection&lt;/strong&gt;.     &lt;br /&gt;the potential memory pressure has reduced by the internal data-structure design.     &lt;br /&gt;so it is definitely something to wait for its release.&lt;/p&gt;  &lt;p&gt;you can read more about it at &lt;a href="http://blogs.msdn.com/b/bclteam/archive/2012/12/18/preview-of-immutable-collections-released-on-nuget.aspx"&gt;here&lt;/a&gt;.     &lt;br /&gt;and if you want more background and comparison to the read only collection, you can check out &lt;a href="http://blogs.msdn.com/b/andrewarnottms/archive/2011/08/22/read-only-frozen-and-immutable-types-and-collections.aspx"&gt;this&lt;/a&gt; post.&lt;/p&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/Immutable-Collections-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2013%2F02%2F17%2Fimmutable-collections.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1751972" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Extensibility/default.aspx">Extensibility</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Composition/default.aspx">Composition</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ImportMany/default.aspx">ImportMany</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/CompositionContainer/default.aspx">CompositionContainer</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/collection/default.aspx">collection</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/BCL/default.aspx">BCL</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Immutable/default.aspx">Immutable</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Thread-safe/default.aspx">Thread-safe</category></item><item><title>MEF 2.0 - mini series: part 8 (Composition options and exception handling)</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/02/16/mef-2-0-mini-series-part-8-composition-options-and-exception-handling.aspx</link><pubDate>Sat, 16 Feb 2013 14:05:57 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1750200</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;MEF 2.0 - mini series: part 8 (Composition options and exception handling)&lt;/h2&gt;  &lt;p&gt;this is the last post in the &lt;strong&gt;MEF 2.0&lt;/strong&gt; mini series.     &lt;br /&gt;you can see other posts of this series in &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/06/mef-2-0-toc.aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;this post will wrap-up the series with a quick survey to to the to some changes made for the &lt;strong&gt;underline composition&lt;/strong&gt; process.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_0389_08C6B15D.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="MEF, SELA, Export, Import, CompositionContainer, RegistrationBuilder, Catalog" border="0" alt="MEF, SELA, Export, Import, CompositionContainer, RegistrationBuilder, Catalog" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_0389_thumb_6D41AF59.jpg" width="449" height="232" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;h5&gt;Exception &lt;/h5&gt;  &lt;p&gt;one of the most painful experience of MEF 1 was its &lt;strong&gt;misleading exception&amp;#39;s description&lt;/strong&gt;. some time it was really hard to figure out the exception roots.&amp;#160; you can read more about MEF 1 issues in &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2011/03/18/sdp-mef-real-life-patterns.aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;MEF 2&lt;/strong&gt; was doing a much better job, but still some composition failure are misleading.     &lt;br /&gt;my recommendation is to use the &lt;strong&gt;IPartImportsSatisfiedNotification&lt;/strong&gt; which can give you an interception point where you can figure out what went wrong, before MEF is throwing a composition exception.&lt;/p&gt;  &lt;p&gt;the pattern that I&amp;#39;m using is to allow default on any imports and validate it on the OnImportsSatisfied method, see the next snippet:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:07fccf84-12ee-405b-9fdf-329cf30fbcdb" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;IPartImportsSatisfiedNotification&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(AllowDefault=&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;true&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;)]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;IIdentity&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Identity { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ImportMany&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;IReflect&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[] Reflects { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; OnImportsSatisfied()&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (Identity == &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Trace&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Composition failure (Identity)&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;throw&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ArgumentNullException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Identity&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (Reflects == &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; || Reflects.Length == 0)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Trace&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Composition failure (Reflects)&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;throw&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ArgumentNullException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Reflects&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;h5&gt;Composition options&lt;/h5&gt;  &lt;p&gt;&lt;strong&gt;MEF 2&lt;/strong&gt; is exposing some &lt;strong&gt;composition tuning options&lt;/strong&gt; that wasn&amp;#39;t available on MEF 1.     &lt;br /&gt;there was some &lt;strong&gt;hidden composition assumptions&lt;/strong&gt; which you couldn&amp;#39;t modified.     &lt;br /&gt;I will focus on the &lt;strong&gt;DisableSilentRejection&lt;/strong&gt; options.     &lt;br /&gt;silent rejection was added into MEF composition in order to support &lt;strong&gt;robust composition&lt;/strong&gt; of &lt;strong&gt;ill functioning plug-ins&lt;/strong&gt;. the requirement for this feature came from the Visual Studio team which is using MEF for their plug-in model.     &lt;br /&gt;anyway you might want to alter this behavior. MEF 2 doe&amp;#39;s let you the option to disable it.     &lt;br /&gt;the original behavior will &lt;strong&gt;exclude&lt;/strong&gt; the ill functioning plug-in from the composition (for import many) without throwing an exception.     &lt;br /&gt;this led to scenarios when you had a real hard time to figure out why some of the plug-in wasn&amp;#39;t loading and still everything seem to be working.&lt;/p&gt;  &lt;p&gt;the following snippet is demonstrating this scenario:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:61b43611-220e-4b89-9463-0416020f0b00" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Program&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Main(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;string&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[] args)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; p = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Program&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; cat = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AssemblyCatalog&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Assembly&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.GetExecutingAssembly());&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; container = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionContainer&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(cat, &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionOptions&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.DisableSilentRejection);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;container.ComposeParts(p);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;{0} Plug-ins loaded&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, p.Plugins.Length);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;catch&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Composition error&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ImportMany&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Iplugin&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[] Plugins { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;InheritedExport&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;interface&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Iplugin&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; { }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;BadPlugin&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Iplugin&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;IIdentity&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;))]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;IIdentity&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; MissingDependency { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;FinePlugin&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Iplugin&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; { }&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;strong&gt;line 8&lt;/strong&gt;: &lt;strong&gt;disabling the silent rejection&lt;/strong&gt; behavior.     &lt;br /&gt;&lt;strong&gt;line 28 - 32&lt;/strong&gt;: a class that is having a &lt;strong&gt;missing dependencies&lt;/strong&gt;, therefore cannot be construct. on silent rejection mode (which is the default) it will be simply &lt;strong&gt;removed from the composition&lt;/strong&gt; and the Plugins property at line 22 will get a single plug-in rather then 2.&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;MEF 2 is having improvements both in the engine and the API level.    &lt;br /&gt;you do have more control over the composition and better descriptive exception.&lt;/p&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/MEF-20-mini-series-part-8-Composition-options-and-exception-handling-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2013%2F02%2F16%2Fmef-2-0-mini-series-part-8-composition-options-and-exception-handling.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1750200" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Composition/default.aspx">Composition</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ImportMany/default.aspx">ImportMany</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/CompositionContainer/default.aspx">CompositionContainer</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/RegistrationBuilder/default.aspx">RegistrationBuilder</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Catalog/default.aspx">Catalog</category></item><item><title>MEF 2.0 - mini series: Part 7 (Catalog filter and Deep hierarchic scoping)</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/24/mef-2-0-mini-series-part-7-catalog-filter-and-deep-hierarchic-scoping.aspx</link><pubDate>Thu, 24 Jan 2013 16:31:18 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1696250</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;MEF 2.0 - mini series: Part 7 (Catalog filter and Deep hierarchic scoping)&lt;/h2&gt;  &lt;p&gt;this is the 7th post in the &lt;strong&gt;MEF 2.0&lt;/strong&gt; mini series.     &lt;br /&gt;you can see the following &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/06/mef-2-0-toc.aspx"&gt;TOC&lt;/a&gt; for other posts in this series.&lt;/p&gt;  &lt;p&gt;in the &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/16/mef-2-0-mini-series-part-6-composition-scoping-and-lifetime-management.aspx" target="_blank"&gt;previous&lt;/a&gt; post I was talking about &lt;strong&gt;composition scoping &lt;/strong&gt;and &lt;strong&gt;lifetime management.&lt;/strong&gt;     &lt;br /&gt;on this one, I will extend the &lt;strong&gt;composition scoping&lt;/strong&gt; topic toward &lt;strong&gt;hierarchic&lt;/strong&gt; along with &lt;strong&gt;catalog filtering&lt;/strong&gt; capability.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_6354_3E65F039.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="MEF, extension, Composition, ImportMany, Export, Import, .NET 4.5, egistrationBuilder, Catalog" border="0" alt="MEF, extension, Composition, ImportMany, Export, Import, .NET 4.5, egistrationBuilder, Catalog" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_6354_thumb_05F7C961.jpg" width="373" height="368" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;hierarchic scoping&lt;/strong&gt; is not trivial, you must understand the hierarchic behavior and what it was design for.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;MEF hierarchic&lt;/strong&gt; was &lt;strong&gt;design&lt;/strong&gt; to enable &lt;strong&gt;parts &lt;/strong&gt;from &lt;strong&gt;higher scope &lt;/strong&gt;to access &lt;strong&gt;lower scope&amp;#39;s part&lt;/strong&gt; without re-instantiation, lower part do not target higher scope&amp;#39;s part directly.&lt;/p&gt;  &lt;p&gt;each hierarchic scope construct by importing a &lt;strong&gt;ExportFactory&amp;lt;T&amp;gt;&lt;/strong&gt; (which we saw in the previous post).&lt;/p&gt;  &lt;p&gt;I will try to make it as simple as I can.&lt;/p&gt;  &lt;p&gt;the following diagram demonstrate the idea of &lt;strong&gt;sub scope&lt;/strong&gt; that &lt;strong&gt;interact with its parent scope&amp;#39;s items&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/SubScope_113DDFA4.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="MEF, extension, Composition, ImportMany, Export, Import, .NET 4.5, egistrationBuilder, Catalog" border="0" alt="MEF, extension, Composition, ImportMany, Export, Import, .NET 4.5, egistrationBuilder, Catalog" src="http://blogs.microsoft.co.il/blogs/bnaya/SubScope_thumb_76251095.png" width="371" height="459" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;let try to have a similar code structures.&lt;/p&gt;  &lt;p&gt;I will use a plug-in base class to trace the instantiations:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:25b11936-2884-4ae7-b73d-702f0d714feb" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;abstract&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginBase&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; _globalId = 0;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;protected&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; _id;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; PluginBase()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;_id = _globalId++; &lt;/span&gt;&lt;span style="background:#ffffff;color:#008000;"&gt;// not thread-safe&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;derived class&amp;#39;s ids will be incremented per instantiation. &lt;/p&gt;  &lt;p&gt;the following snippet include the classes which will be used for our hierarchic demonstration:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b4a6d890-4976-4cfd-b8c1-4a8ed9535156" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level1ScopeRoot&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginBase&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level1ItemA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ItemA { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level1ItemB&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ItemB { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level1ItemShared&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginBase&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; { }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level1ItemA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginBase&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level1ItemShared&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ItemZ { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level1ItemB&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginBase&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level1ItemShared&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; SharedItem { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ExportFactory&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level2SubScopeRoot&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; SubScope { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level2SubScopeRoot&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginBase&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level2SubItem1&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; SubItem1 { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level2SubItem2&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; SubItem2 { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level2SubItem1&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginBase&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level1ItemShared&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ItemShared { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level2SubItemShared&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; SubItemShared { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level2SubItem2&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginBase&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level2SubItemShared&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; SubItem { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level2SubItemShared&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginBase&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; { }&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;all the classes prefixed with the scoping &amp;#39;&lt;strong&gt;Level&lt;/strong&gt;&amp;#39;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;referring a sub-scope&lt;/strong&gt; required using of &lt;strong&gt;ExportFactory&amp;lt;T&amp;gt; &lt;/strong&gt;(line 23).&lt;/p&gt;  &lt;p&gt;the following diagram is showing their hierarchic and dependencies graph:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/SubScope1_0D03E212.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="MEF, extension, Composition, ImportMany, Export, Import, .NET 4.5, egistrationBuilder, Catalog" border="0" alt="MEF, extension, Composition, ImportMany, Export, Import, .NET 4.5, egistrationBuilder, Catalog" src="http://blogs.microsoft.co.il/blogs/bnaya/SubScope1_thumb_23E2B38E.png" width="487" height="457" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;our final step is to define a &lt;strong&gt;catalog per scope&amp;#39;s hierarchic&lt;/strong&gt; and to &lt;strong&gt;set hierarchic structure&lt;/strong&gt;.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:cb185349-f5b2-4c6e-a386-5622630e500b" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; picker = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;RegistrationBuilder&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;picker.ForTypesDerivedFrom&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginBase&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#008000;"&gt;// add ScopeLevel metadata &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.AddMetadata(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;ScopeLevel&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, t =&amp;gt; t.Name.StartsWith(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Level1&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;) ? 1 : 2)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.Export&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginBase&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; catalog = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AssemblyCatalog&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Program&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;).Assembly, picker);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; catalogL0 = catalog.Filter(p =&amp;gt; p.ContainsPartMetadata(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;ScopeLevel&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, 1));&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; catalogL1 = catalog.Filter(p =&amp;gt; p.ContainsPartMetadata(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;ScopeLevel&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, 2));&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; scopeL1 = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionScopeDefinition&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(catalogL1, &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; scopeL0 = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionScopeDefinition&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(catalogL0, &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[] { scopeL1 });&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; container = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionContainer&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(scopeL0);&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;at line 2-5, I &lt;strong&gt;export&lt;/strong&gt; &lt;strong&gt;everything inherits from &amp;#39;PluginBase&amp;#39;&lt;/strong&gt; and &lt;strong&gt;adding a &amp;#39;ScopeLevel&amp;#39; metadata&lt;/strong&gt; with the right scope leveling value.&lt;/p&gt;  &lt;p&gt;line 7, define a general &lt;strong&gt;assembly catalog&lt;/strong&gt; (which will be the base for the filtered catalogs).&lt;/p&gt;  &lt;p&gt;lines 9,10, are defining a &lt;strong&gt;filtered catalog&lt;/strong&gt; for each scoping level (filtering an existing assembly catalog).&lt;/p&gt;  &lt;p&gt;lines 12,13, &lt;strong&gt;defines&lt;/strong&gt; the &lt;strong&gt;hierarchic&lt;/strong&gt; between the scoped catalogs.&lt;/p&gt;  &lt;p&gt;line 15, create a &lt;strong&gt;container&lt;/strong&gt; with the top level scope.&lt;/p&gt;  &lt;p&gt;now we&amp;#39;re ready for a composition.&lt;/p&gt;  &lt;p&gt;&lt;a href="https://skydrive.live.com/?cid=9bf7c1a515d76a9a&amp;amp;id=9BF7C1A515D76A9A%216037" target="_blank"&gt;here&lt;/a&gt; you can download a &lt;a href="https://skydrive.live.com/?cid=9bf7c1a515d76a9a&amp;amp;id=9BF7C1A515D76A9A%216037" target="_blank"&gt;sample code&lt;/a&gt;.&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;I consider this feature as &lt;strong&gt;MEF 2&lt;/strong&gt;&amp;#39;s most complex one.&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Hierarchic&lt;/strong&gt; can become complex, so use it with caution.&lt;/p&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/MEF-20-mini-series-Part-7-Catalog-filter-and-Deep-hierarchic-scoping-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2013%2F01%2F24%2Fmef-2-0-mini-series-part-7-catalog-filter-and-deep-hierarchic-scoping.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1696250" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Composition/default.aspx">Composition</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ImportMany/default.aspx">ImportMany</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Metadata/default.aspx">Metadata</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/.NET+4.5/default.aspx">.NET 4.5</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/exception/default.aspx">exception</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Catalog/default.aspx">Catalog</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/egistrationBuilder/default.aspx">egistrationBuilder</category></item><item><title>MEF 2.0 - mini series: part 6 (Composition scoping and lifetime management)</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/16/mef-2-0-mini-series-part-6-composition-scoping-and-lifetime-management.aspx</link><pubDate>Wed, 16 Jan 2013 06:21:01 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1667233</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h3&gt;MEF 2.0 - mini series: part 6 &lt;/h3&gt;  &lt;h3&gt;(Composition scoping and lifetime management)&lt;/h3&gt;  &lt;p&gt;this is the 6th post in the &lt;strong&gt;MEF 2.0&lt;/strong&gt; mini series.     &lt;br /&gt;you can see the following &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/06/mef-2-0-toc.aspx"&gt;TOC&lt;/a&gt; for other posts in this series.&lt;/p&gt;  &lt;p&gt;in this post I will cover a new concept of &lt;strong&gt;scoping&lt;/strong&gt; and &lt;strong&gt;part lifetime management&lt;/strong&gt;, which is a great improvement over MEF 1.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_2080_614BDBED.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="DEV, MEF, SELA, Export, Import, CompositionContainer, RegistrationBuilder, Catalog" border="0" alt="DEV, MEF, SELA, Export, Import, CompositionContainer, RegistrationBuilder, Catalog" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_2080_thumb_3A9D4FA0.jpg" width="471" height="324" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;MEF 1&lt;/strong&gt; was coming with a fairly &lt;strong&gt;naïve lifetime management&lt;/strong&gt;.     &lt;br /&gt;part&amp;#39;s lifetime could be either shared or non-shared (&lt;font color="#666666"&gt;you could also apply &amp;#39;any&amp;#39; but eventually &amp;#39;any&amp;#39; will be created as shared or non-shared&lt;/font&gt;).     &lt;br /&gt;shared is a singleton instantiation, while non-shared will create a new instance each time.&lt;/p&gt;  &lt;p&gt;MEF 1&amp;#39;s instantiation model doesn&amp;#39;t support a complex scenario where some dependency&amp;#39;s lifetime should be dictate by the lifetime of other unites.    &lt;br /&gt;you can conceder a UI window that is having plug-ins that should be dispose while the UI window is closing. &lt;/p&gt;  &lt;p&gt;for example consider you have the following components (parts) dependency flow:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/Presentation1_4CBB76C0.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="DEV, MEF, SELA, Export, Import, CompositionContainer, RegistrationBuilder, Catalog" border="0" alt="DEV, MEF, SELA, Export, Import, CompositionContainer, RegistrationBuilder, Catalog" src="http://blogs.microsoft.co.il/blogs/bnaya/Presentation1_thumb_36A4E561.jpg" width="418" height="235" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;the application can have multiple processes.    &lt;br /&gt;each process is having some plugins, but each view should have different instantiation of the plugins.     &lt;br /&gt;until now you can argue that a non-shared instantiation will do the job.     &lt;br /&gt;the next dependency level (DAL) &lt;strong&gt;should be shared under the boundary of a process&lt;/strong&gt; but &lt;strong&gt;shouldn&amp;#39;t be shared across processes&lt;/strong&gt;.&amp;#160; &lt;br /&gt;this one can neither handle by the share nor by non-shared instantiation.     &lt;br /&gt;single instance of DAL should be created under each view scope.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;MEF 2&lt;/strong&gt; added a &lt;strong&gt;scoped lifetime management&lt;/strong&gt;.     &lt;br /&gt;I will use the&lt;strong&gt; attribute based model&lt;/strong&gt; for this sample, but it will work in the same way in the &lt;strong&gt;fluent model&lt;/strong&gt;.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:bbded519-b190-4131-a26d-a23dc52527e3" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Application&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ExportFactory&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; ProcAFactory { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ExportFactory&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessB&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; ProcBFactory { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; PlugA { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginB&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; PlugB { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessB&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; PlugA { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginB&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; PlugB { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;DAL&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Dal { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginB&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;DAL&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Dal { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;DAL&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;at line 4-7 you can see a &lt;strong&gt;new type of importing target&lt;/strong&gt; called &lt;strong&gt;ExportFactory&amp;lt;T&amp;gt;&lt;/strong&gt; this type will initialize the scope.     &lt;br /&gt;It is having a &lt;strong&gt;CreateExport&lt;/strong&gt; method that return a &lt;strong&gt;ExportLifetimeContext&amp;lt;T&amp;gt;&lt;/strong&gt; can control the part life time.&lt;/p&gt;  &lt;p&gt;the full implementation of the Application class is:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:28b41930-c6ff-4a1b-ad02-fbbd478b7d27" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;  &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;  &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Application&lt;/span&gt;&lt;/li&gt; &lt;li&gt;  &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;      &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ExportFactory&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; ProcAFactory { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;      &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ExportFactory&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessB&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; ProcBFactory { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;      &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; WriteLayoutA()&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;      &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;          &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;using&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ExportLifetimeContext&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; lifeOfA = ProcAFactory.CreateExport())&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;          &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;              &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; a = lifeOfA.Value;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;              &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Proc A&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;              &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;&amp;#92;tPlug A: {0}&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, a.PlugA.GetHashCode());&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;              &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;&amp;#92;t&amp;#92;tDal: {0}&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, a.PlugA.Dal.GetHashCode());&lt;/span&gt;&lt;/li&gt; &lt;li&gt;              &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;&amp;#92;tPlug B: {0}&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, a.PlugB.GetHashCode());&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;              &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;&amp;#92;t&amp;#92;tDal: {0}&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, a.PlugB.Dal.GetHashCode());&lt;/span&gt;&lt;/li&gt; &lt;li&gt;          &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;      &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;      &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; WriteLayoutB()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;          &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;using&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ExportLifetimeContext&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessB&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; lifeOfB = ProcBFactory.CreateExport())&lt;/span&gt;&lt;/li&gt; &lt;li&gt;          &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;                &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessB&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;b = lifeOfB.Value;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;              &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Proc B&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;              &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;&amp;#92;tPlug A: {0}&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, b.PlugA.GetHashCode());&lt;/span&gt;&lt;/li&gt; &lt;li&gt;              &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;&amp;#92;t&amp;#92;tDal: {0}&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, b.PlugA.Dal.GetHashCode());&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;              &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;&amp;#92;tPlug B: {0}&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, b.PlugB.GetHashCode());&lt;/span&gt;&lt;/li&gt; &lt;li&gt;              &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;&amp;#92;t&amp;#92;tDal: {0}&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, b.PlugB.Dal.GetHashCode());&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;          &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;  &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;you can see the usage of &lt;strong&gt;ExportFactory&amp;lt;T&amp;gt;&lt;/strong&gt; at lines 11 and 24.&lt;/p&gt;  &lt;p&gt;having a &lt;strong&gt;lifetime handled part&lt;/strong&gt; is the &lt;strong&gt;first step for the scoping&lt;/strong&gt;.     &lt;br /&gt;now we can define which part will be managed by the scope.     &lt;br /&gt;for each scope we except to find a single instantiation of each scoped part.     &lt;br /&gt;as you can see in the diagram below, the Plugins and the DAL should be instantiate once for each scope.     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/Presentation1_4D1783E8.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="Presentation1" border="0" alt="Presentation1" src="http://blogs.microsoft.co.il/blogs/bnaya/Presentation1_thumb_376D257E.jpg" width="429" height="241" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;in order to define what&amp;#39;s goes within the scope we should use the &lt;strong&gt;CompositionScopeDefinition&lt;/strong&gt; as shown in the following code snippet:&lt;/p&gt;    &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c29f7814-3e2a-4939-9ad9-13a2a6f52938" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Main(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;string&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[] args)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; scopeDependentCatalog = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;TypeCatalog&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;),&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessB&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;), &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;),&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginB&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;),&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;DAL&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;));&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; scopeDefDependent = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionScopeDefinition&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(scopeDependentCatalog, &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; appCatalog = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;TypeCatalog&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Application&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;));&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; scopeDefRoot = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionScopeDefinition&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(appCatalog, &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[] { scopeDefDependent });&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; container = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionContainer&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(scopeDefRoot);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; app = container.GetExportedValue&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Application&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;app.WriteLayoutA();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;------------------------------------&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;app.WriteLayoutB();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;    &lt;p&gt;you can see the &lt;strong&gt;scope dependent definition&lt;/strong&gt; at line 9     &lt;br /&gt;and the &lt;strong&gt;hierarchic&lt;/strong&gt; between the scope and the application at line 12.&lt;/p&gt;  &lt;p&gt;as you may notice it is possible to have deeper hierarchic, but this will be shown in future posts.&lt;/p&gt;  &lt;p&gt;the output is:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/scopeoutput_652AC876.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="scopeoutput" border="0" alt="scopeoutput" src="http://blogs.microsoft.co.il/blogs/bnaya/scopeoutput_thumb_2ACF5295.png" width="337" height="235" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;remember that we was printing the &lt;strong&gt;part&amp;#39;s hash code&lt;/strong&gt;.     &lt;br /&gt;you can easily see that each process is having different plugin and DAL&amp;#39;s instantiations, but even those the DAL was consumed by both plugins it has a single instantiation per scope.&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;scoping is a very powerful instantiation model which can solve some of the real-life scenario which wasn&amp;#39;t fall into the shared or non-shared models.&lt;/p&gt;  &lt;p&gt;in future post I will present a deeper hierarchic and the catalog filtering capability.&lt;/p&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/MEF-20-mini-series-part-6-Composition-scoping-and-lifetime-management-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2013%2F01%2F16%2Fmef-2-0-mini-series-part-6-composition-scoping-and-lifetime-management.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1667233" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Composition/default.aspx">Composition</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ImportMany/default.aspx">ImportMany</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/.NET+4.5/default.aspx">.NET 4.5</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/CompositionContainer/default.aspx">CompositionContainer</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/RegistrationBuilder/default.aspx">RegistrationBuilder</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Catalog/default.aspx">Catalog</category></item><item><title>MEF 2.0 - mini series: part 5 (Fluent export properties)</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/15/mef-2-0-mini-series-part-5-fluent-export-properties.aspx</link><pubDate>Tue, 15 Jan 2013 06:04:54 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1663793</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;MEF 2.0 - mini series: part 5 (Fluent export properties)&lt;/h2&gt;  &lt;p&gt;this is the 5th post in the &lt;strong&gt;MEF 2.0&lt;/strong&gt; mini series.     &lt;br /&gt;you can see the following &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/06/mef-2-0-toc.aspx"&gt;TOC&lt;/a&gt; for other posts in this series.&lt;/p&gt;  &lt;p&gt;in this post I will cover the &lt;strong&gt;fluent property&amp;#39;s export&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_8047_6004ACAC.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="MEF, Import, Export, CompositionContainer, RegistrationBuilder, Catalog" border="0" alt="MEF, Import, Export, CompositionContainer, RegistrationBuilder, Catalog" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_8047_thumb_441377B4.jpg" width="533" height="420" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Exporting properties&lt;/strong&gt; is a less known feature of &lt;strong&gt;MEF&lt;/strong&gt;.     &lt;br /&gt;MEF 1 was supporting this feature by using the attribute model.     &lt;br /&gt;you could decorate a property with a &lt;strong&gt;[Export]&lt;/strong&gt; attribute and then it become available for imports.&lt;/p&gt;  &lt;p&gt;the following code demonstrate property exporting in &lt;strong&gt;MEF 1&lt;/strong&gt;:&lt;/p&gt;  &lt;p&gt;the Foo class is importing multiple SymmetricAlgorithm:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b0602047-a06b-4f28-b86b-e676c745744e" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ImportMany&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[] CryptoProviders { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;and the CryptoComposer class is exporting a few symmetric algorithms:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:31387df1-d35c-4c93-a7ee-5e2c44cf671b" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CryptoComposer&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Aes { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AesManaged&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); } }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; TripleDES { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;TripleDESCryptoServiceProvider&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); } }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; RC2 { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;RC2CryptoServiceProvider&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); } }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;with the following composition the Foo instance will have the CryptoComposer&amp;#39;s symmetric algorithms:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3d1ef620-a957-4e7e-86d9-27990ba28cb0" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; catalog = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AssemblyCatalog&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Program&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;).Assembly);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; container = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionContainer&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(catalog);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; foo = container.GetExportedValue&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;();&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;h5&gt;so, how do we do it with the fluent export API?&lt;/h5&gt;  &lt;p&gt;to keep it simple I will leave the Foo class with the attribute model (we learned how to use &lt;strong&gt;fluent import&lt;/strong&gt; in the previous &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/12/mef-2-0-mini-series-part-4-fluent-import.aspx" target="_blank"&gt;post&lt;/a&gt;):&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e2fcb7bc-b2db-49d9-ab32-ddbedd73912a" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ImportMany&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[] CryptoProviders { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CryptoComposer&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Aes { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AesManaged&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); } }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; TripleDES { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;TripleDESCryptoServiceProvider&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); } }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; RC2 { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;RC2CryptoServiceProvider&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); } }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;in order to export the CryptoComposer&amp;#39;s properties you should use the following code:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3e993dad-1510-4ea9-a111-a7325b56d7ca" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; picker = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;RegistrationBuilder&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;picker.ForType&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CryptoComposer&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.ExportProperties(p =&amp;gt; p.PropertyType == &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;));&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; catalog = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AssemblyCatalog&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Program&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;).Assembly, picker);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; container = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionContainer&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(catalog);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; foo = container.GetExportedValue&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;();&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;h5&gt;summary&lt;/h5&gt;  &lt;p&gt;exporting properties using the &lt;strong&gt;fluent API&lt;/strong&gt; is fairly straightforward.&lt;/p&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/MEF-20-mini-series-part-5-Fluent-export-properties-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2013%2F01%2F15%2Fmef-2-0-mini-series-part-5-fluent-export-properties.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1663793" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Composition/default.aspx">Composition</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ImportMany/default.aspx">ImportMany</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/CompositionContainer/default.aspx">CompositionContainer</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/RegistrationBuilder/default.aspx">RegistrationBuilder</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Catalog/default.aspx">Catalog</category></item><item><title>MEF 2.0 - mini series: part 4 (Fluent Import)</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/12/mef-2-0-mini-series-part-4-fluent-import.aspx</link><pubDate>Sat, 12 Jan 2013 05:18:51 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1654744</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;MEF 2.0 - mini series: part 4 (Fluent Import)&lt;/h2&gt; this is the 4th post in the &lt;strong&gt;MEF 2.0&lt;/strong&gt; mini series.   &lt;br /&gt;you can see the following &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/06/mef-2-0-toc.aspx"&gt;TOC&lt;/a&gt; for other posts in this series.   &lt;p&gt;in this post I will discuss the &lt;strong&gt;Fluent import API&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_6341_610CADF0.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="MEF, Import, Export, CompositionContainer, RegistrationBuilder, Catalog" border="0" alt="MEF, Import, Export, CompositionContainer, RegistrationBuilder, Catalog" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_6341_thumb_57643FBA.jpg" width="353" height="445" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;we was talking about&lt;strong&gt; Fluent Export&lt;/strong&gt; and&lt;strong&gt; Constructor Injection&lt;/strong&gt; in the previous posts. this post will &lt;strong&gt;complete the main scenarios &lt;/strong&gt;of &lt;strong&gt;Import&amp;#39;s fluent API&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;I will demonstrate the &lt;strong&gt;import technique&lt;/strong&gt; using the following classes:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:52b48dc1-c894-4a5e-afbf-ed2a5ecbc697" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Logger&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Log { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Logger&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Write(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;string&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; message) { &lt;/span&gt;&lt;span style="background:#ffffff;color:#008000;"&gt;/* ... */&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;we can &lt;strong&gt;Export&lt;/strong&gt; both classes by one of the &lt;strong&gt;fluent techniques&lt;/strong&gt; which we discuss in previous posts, for example:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:95f932ca-e083-4ee3-85c6-bf058d988817" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; picker = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;RegistrationBuilder&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;picker.ForType&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Logger&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.Export&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Logger&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;picker.ForType&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.Export();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; catalog = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AssemblyCatalog&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;).Assembly, picker);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; container = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionContainer&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(catalog);&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;but this is not enough, because Foo&amp;#39;s Log property has no &lt;strong&gt;[Import]&lt;/strong&gt; attribute, therefore it will be ignored by the composition.&lt;/p&gt;  &lt;p&gt;using the fluent &lt;strong&gt;ImportProperties&lt;/strong&gt;, you can introduce it into the composition :&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1f6a9ad4-14be-4159-83d0-7c1f0992f22a" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;picker.ForType&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;()&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.Export()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.ImportProperties&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Logger&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;(pi =&amp;gt; pi.Name == &lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Log&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;the &lt;strong&gt;property&amp;#39;s Type&lt;/strong&gt; is referred by the &lt;strong&gt;generics parameter&lt;/strong&gt; while the &lt;strong&gt;Lambda &lt;/strong&gt;used for &lt;strong&gt;filtering&lt;/strong&gt; the target properties.&lt;/p&gt;  &lt;p&gt;actually you can take it further and &lt;strong&gt;configure&lt;/strong&gt; the &lt;strong&gt;import behaviors&lt;/strong&gt;.     &lt;br /&gt;for example you can set the import to &lt;strong&gt;Allow Default&lt;/strong&gt;:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4869ce79-d7d4-44dc-971e-467670378247" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;picker.ForType&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;()&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.Export()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.ImportProperties&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Logger&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;(pi =&amp;gt; pi.Name == &lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Log&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;,&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                &lt;span style="background:#ffffff;color:#000000;"&gt;(propInfo, builder) =&amp;gt; builder.AllowDefault());&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;it&amp;#39;s consider as&lt;strong&gt; best practice&lt;/strong&gt; to &lt;strong&gt;Allow Default&lt;/strong&gt; and implement &lt;strong&gt;IPartImportsSatisfiedNotification&lt;/strong&gt; interface which is an interception point which occurs right after the composition.     &lt;br /&gt;the idea is to check the composition state within the &lt;strong&gt;OnImportsSatisfied()      &lt;br /&gt;&lt;/strong&gt;method in order to verify the composition state.     &lt;br /&gt;It is true that &lt;strong&gt;MEF&lt;/strong&gt; &lt;strong&gt;exceptions&lt;/strong&gt; have &lt;strong&gt;made better&lt;/strong&gt;, but it still not perfect and sometimes it is really &lt;strong&gt;hard to trace the root cause&lt;/strong&gt; of the exception. implementation of &lt;strong&gt;IPartImportsSatisfiedNotification&lt;/strong&gt; will make the tracing easier.&lt;/p&gt;  &lt;h5&gt;so what about Select Many?&lt;/h5&gt;  &lt;p&gt;let change our Foo class to the following code:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f7fa8ef2-3619-4c99-909d-af9180765210" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;: &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;IPartImportsSatisfiedNotification&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Logger&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[] Log { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; OnImportsSatisfied()&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;the following code will define the Log property as [ImportMany]:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:5e7e5a55-560a-43af-8760-ba76bb36e721" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;picker.ForType&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;()&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.Export()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.ImportProperties&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Logger&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;(pi =&amp;gt; pi.Name == &lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Log&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;,&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                &lt;span style="background:#ffffff;color:#000000;"&gt;(propInfo, builder) =&amp;gt; builder.AsMany());&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;as you can see we are using the same &lt;strong&gt;overload&lt;/strong&gt; that can manipulate the &lt;strong&gt;Import&amp;#39;s behaviors&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;by the same technique you can define a &lt;strong&gt;re-composition&lt;/strong&gt; and the &lt;strong&gt;creation&lt;/strong&gt; &lt;strong&gt;policy&lt;/strong&gt;:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:376c77c8-6835-4ddb-8c7a-74c90f155c80" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;picker.ForType&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;()&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.Export()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.ImportProperties&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Logger&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;(pi =&amp;gt; pi.Name == &lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Log&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;,&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                &lt;span style="background:#ffffff;color:#000000;"&gt;(propInfo, builder) =&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                    &lt;span style="background:#ffffff;color:#000000;"&gt;builder.AsMany();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                    &lt;span style="background:#ffffff;color:#000000;"&gt;builder.AllowRecomposition();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                    &lt;span style="background:#ffffff;color:#000000;"&gt;builder.RequiredCreationPolicy(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CreationPolicy&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.NonShared);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="background:#ffffff;color:#000000;"&gt;});&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;h5&gt;summary&lt;/h5&gt;  &lt;p&gt;fluent &lt;strong&gt;Import&lt;/strong&gt; is complimentary to fluent &lt;strong&gt;Export&lt;/strong&gt;.     &lt;br /&gt;it&amp;#39;s fairly flexible API which enable to assign your own or 3rd party class&amp;#39;s properties.&lt;/p&gt;  &lt;p&gt;actually you can take a 3rd party class and inject it&amp;#39;s public properties through those techniques. &lt;/p&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/MEF-20-mini-series-part-4-Fluent-Import-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2013%2F01%2F12%2Fmef-2-0-mini-series-part-4-fluent-import.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1654744" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Composition/default.aspx">Composition</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/CompositionContainer/default.aspx">CompositionContainer</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/RegistrationBuilder/default.aspx">RegistrationBuilder</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Catalog/default.aspx">Catalog</category></item><item><title>MEF 2.0 - mini series: part 3 (Fluent import constructor)</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/09/mef-2-0-mini-series-part-3-fluent-import-constructor.aspx</link><pubDate>Wed, 09 Jan 2013 21:09:10 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1646157</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;MEF 2.0 - mini series: part 3 (Fluent import constructor)&lt;/h2&gt;  &lt;p&gt;this is the 3rd post in the &lt;strong&gt;MEF 2.0&lt;/strong&gt; mini series.     &lt;br /&gt;you can see the following &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/06/mef-2-0-toc.aspx"&gt;TOC&lt;/a&gt; for other posts in this series.&lt;/p&gt;  &lt;p&gt;in this post I will discuss the &lt;strong&gt;fluent constructor injection API&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_6783_1C550BBA.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="MEF, Import, Export, CompositionContainer, RegistrationBuilder, Catalog" border="0" alt="MEF, Import, Export, CompositionContainer, RegistrationBuilder, Catalog" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_6783_thumb_24893151.jpg" width="478" height="319" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;h5&gt;Constructor Injection&lt;/h5&gt;  &lt;p&gt;the following code is having a Logger and &lt;strong&gt;Worker&lt;/strong&gt; class which is having 2 &lt;strong&gt;constructor&lt;/strong&gt; (one has no parameters and the other is getting &lt;strong&gt;ILogger&lt;/strong&gt;):&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:160487bb-95c7-4c20-8612-ee854e0eb978" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Logger&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;: &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ILogger&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Write(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;string&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; message) { &lt;/span&gt;&lt;span style="background:#ffffff;color:#008000;"&gt;/* ... */}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Worker&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ILogger&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; _logger;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Worker()&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#008000;"&gt;/* ... */&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Worker(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ILogger&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; logger)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;_logger = logger;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;} &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;the question is, which of the construction will be invoked by the following code?&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:aab94810-3d80-4034-ae48-eb4b9de654da" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; picker = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;RegistrationBuilder&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;picker.ForTypesDerivedFrom&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ILogger&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.Export&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ILogger&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;picker.ForType&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Worker&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.Export();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; catalog = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AssemblyCatalog&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Program&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;).Assembly, picker);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; container = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionContainer&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(catalog);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;container.Compose(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionBatch&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;());&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; w = container.GetExportedValue&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Worker&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;();&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;the code &lt;strong&gt;Export&lt;/strong&gt; the &lt;strong&gt;Logger&lt;/strong&gt; and the &lt;strong&gt;Worker&lt;/strong&gt; but doesn&amp;#39;t define explicitly which of the Worker&amp;#39;s construction should be invoke.&lt;/p&gt;  &lt;p&gt;the answer is that it will invoke the constructor that is getting the ILogger parameter, but could we be more precise?&lt;/p&gt;  &lt;p&gt;actually we do have 2 different options.&lt;/p&gt;  &lt;p&gt;first option is to use &lt;strong&gt;SelectConstructor&lt;/strong&gt; before the &lt;strong&gt;Export &lt;/strong&gt;method.     &lt;br /&gt;see the following code:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:53f5067f-9f19-4940-83b5-24f429207900" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; picker = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;RegistrationBuilder&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;picker.ForTypesDerivedFrom&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ILogger&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.Export&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ILogger&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;picker.ForType&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Worker&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.SelectConstructor(builder =&amp;gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Worker&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(builder.Import&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ILogger&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;()))&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.Export();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; catalog = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AssemblyCatalog&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Program&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;).Assembly, picker);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; container = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionContainer&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(catalog);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;container.Compose(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionBatch&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;());&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; w = container.GetExportedValue&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Worker&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;();&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;this way we can define an expression that define which constructor should be invoke.    &lt;br /&gt;the expression is getting a &lt;strong&gt;ParameterImportBuilder&lt;/strong&gt; which I used to define the logger (&lt;strong&gt;builder.Import&amp;lt;ILogger&amp;gt;()&lt;/strong&gt;).&lt;/p&gt;  &lt;p&gt;the next option is to set the &lt;strong&gt;SelectConstructor&lt;/strong&gt; after the &lt;strong&gt;Export&lt;/strong&gt;.     &lt;br /&gt;now we are getting a &lt;strong&gt;ConstructorInfo&lt;/strong&gt;&lt;strong&gt;[]&lt;/strong&gt; and we can select a single constructor out of this array.     &lt;br /&gt;&lt;strong&gt;ConstructorInfo &lt;/strong&gt;&lt;/p&gt;    &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4d7d93f1-f376-486a-9d35-710fda3df3d6" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; picker = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;RegistrationBuilder&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;picker.ForTypesDerivedFrom&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ILogger&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;().Export&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ILogger&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;picker.ForType&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Worker&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;()&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.Export()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.SelectConstructor(ctors =&amp;gt; &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;ctors.First(info =&amp;gt; info.GetParameters().Length == 1));&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;    &lt;p&gt;the code select a constructor which is having a single parameter, but the real implementation is up to you. you can ask on whatever constructor&amp;#39;s aspect which you like to.&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;as cool as it is to control the &lt;strong&gt;constructor injection &lt;/strong&gt;using the fluent API,     &lt;br /&gt;you may &lt;strong&gt;consider to use imported properties&lt;/strong&gt; rather than constructor injection because it can be more stable is during the composition state and can apply on more scenarios, including implementation of &lt;strong&gt;IPartImportsSatisfiedNotification&lt;/strong&gt;.&lt;/p&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/MEF-20-mini-series-part-3-Fluent-import-constructor-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2013%2F01%2F09%2Fmef-2-0-mini-series-part-3-fluent-import-constructor.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1646157" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Composition/default.aspx">Composition</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/CompositionContainer/default.aspx">CompositionContainer</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/RegistrationBuilder/default.aspx">RegistrationBuilder</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Catalog/default.aspx">Catalog</category></item><item><title>MEF 2.0 - mini series: part 2 (Fluent and Conventional Export)</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/06/mef-2-0-mini-series-part-2-fluent-and-conventional-export.aspx</link><pubDate>Sun, 06 Jan 2013 14:51:05 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1634570</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;MEF 2.0 - mini series: part 2 (Fluent and Conventional Export)&lt;/h2&gt;  &lt;p&gt;this is the second post in the &lt;strong&gt;MEF 2.0&lt;/strong&gt; mini series.     &lt;br /&gt;you can see the following &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/06/mef-2-0-toc.aspx" target="_blank"&gt;TOC&lt;/a&gt; for more.&lt;/p&gt;  &lt;p&gt;in this post I will start to cover the &lt;strong&gt;fluent discovery model&lt;/strong&gt; and the idea of &lt;strong&gt;discovery by convention&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_9224_37D5A692.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="MEF, Import, Export, CompositionContainer, RegistrationBuilder, Catalog" border="0" alt="MEF, Import, Export, CompositionContainer, RegistrationBuilder, Catalog" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_9224_thumb_6E6351D6.jpg" width="519" height="405" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;traditionally&lt;/strong&gt;, out of the box, &lt;strong&gt;MEF&lt;/strong&gt; come with a single &lt;strong&gt;discovery model&lt;/strong&gt;, which was the &lt;strong&gt;Attribute model&lt;/strong&gt;. &lt;/p&gt;  &lt;p&gt;it is true that you could &lt;strong&gt;extend MEF&amp;#39;s discovery model&lt;/strong&gt; by using either a &lt;strong&gt;custom catalog&lt;/strong&gt; or a &lt;strong&gt;custom export provider&lt;/strong&gt;, but this task wasn&amp;#39;t as simple as the &lt;strong&gt;MEF 2 fluent discovery model&lt;/strong&gt; and therefore wasn&amp;#39;t a common practice.&lt;/p&gt;  &lt;h5&gt;Registration Builder&lt;/h5&gt;  &lt;p&gt;&lt;strong&gt;MEF 2.0&lt;/strong&gt; is&amp;#160; having the new &lt;strong&gt;RegistrationBuilder&lt;/strong&gt; class which can be used to apply a &lt;strong&gt;custom picker &lt;/strong&gt;(filter) for &lt;strong&gt;specific&lt;/strong&gt; &lt;strong&gt;types&lt;/strong&gt; within the&lt;strong&gt; scope&lt;/strong&gt; of a&lt;strong&gt; catalog&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;the idea is that the &lt;strong&gt;catalog&lt;/strong&gt; will apply a &lt;strong&gt;sandbox&lt;/strong&gt; which is a &lt;strong&gt;searching area scope&lt;/strong&gt; (like Assembly, Directory, ext...), while the &lt;strong&gt;registration builder&lt;/strong&gt; will &lt;strong&gt;pick up&lt;/strong&gt; the relevant &lt;strong&gt;exported part&lt;/strong&gt; within the scope.&lt;/p&gt;  &lt;p&gt;actually the catalog will compose both the old attribute model and the picked up types.&lt;/p&gt;  &lt;p&gt;this technique can be used either as a replacement for the Attribute model or as a complementary for &lt;strong&gt;3rd party &lt;/strong&gt;types which we cannot recompile with the [Export] attribute.&lt;/p&gt;  &lt;p&gt;in order to &lt;strong&gt;work with&lt;/strong&gt; the &lt;strong&gt;registration builder&lt;/strong&gt;, you should add reference to &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;System.Reflection.Context &lt;/li&gt;    &lt;li&gt;System.ComponentModel.Composition &lt;/li&gt;    &lt;li&gt;System.ComponentModel.Composition.Registration &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;at its simplest form it can be used to append a specific type into the composition.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:20e647f4-a326-4b38-ac15-4fd5e071d51e" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Main(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;string&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[] args)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Program&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; p = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Program&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; picker = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;RegistrationBuilder&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;picker.ForType&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AesManaged&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;.Export&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; catalog = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;TypeCatalog&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; []{&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AesManaged&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;)}, picker);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; container = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionContainer&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(catalog);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;container.ComposeParts(p);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Crypto { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;   &lt;br /&gt;the above code &lt;strong&gt;exporting&lt;/strong&gt; the &lt;strong&gt;AesManaged cryptography algorithm&lt;/strong&gt; as a &lt;strong&gt;symmetric crypto algorithm&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;currently the &lt;strong&gt;Import&lt;/strong&gt; is still using the &lt;strong&gt;old Attribute model&lt;/strong&gt;. I will show you how to define a fluent Imports in future post.&lt;/p&gt;  &lt;p&gt;another aspect that you can control is the &lt;strong&gt;instantiation strategy&lt;/strong&gt; by using the &lt;strong&gt;SetCreationPolicy&lt;/strong&gt;:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:39375d85-6951-4df0-b22d-9bff527020d2" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; picker = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;RegistrationBuilder&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;picker.ForType&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AesManaged&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.Export&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;()&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.SetCreationPolicy(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CreationPolicy&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.NonShared);&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;in future post I will talk about&lt;strong&gt; creation scope&lt;/strong&gt; and &lt;strong&gt;lifetime&lt;/strong&gt;, which is a fine tuning option for the part lifetime control.&lt;/p&gt;  &lt;h5&gt;Export inheritance&lt;/h5&gt;  &lt;p&gt;you can choose to &lt;strong&gt;export&lt;/strong&gt; anything that &lt;strong&gt;derived&lt;/strong&gt; from a &lt;strong&gt;Type&lt;/strong&gt; or &lt;strong&gt;implement&lt;/strong&gt; an &lt;strong&gt;Interface&lt;/strong&gt;:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d362d90d-56f8-4414-b8c1-b874eb6545b7" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;picker.ForTypesDerivedFrom&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;()&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.Export&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.SetCreationPolicy(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CreationPolicy&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.NonShared);&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;h5&gt;Export by conventions&lt;/h5&gt;  &lt;p&gt;a common&lt;strong&gt; IoC strategy&lt;/strong&gt; is to &lt;strong&gt;discover extensions by convention&lt;/strong&gt;.     &lt;br /&gt;for example loading all the type with a &amp;#39;Plugin&amp;#39; suffix.     &lt;br /&gt;this strategy &lt;strong&gt;reduce&lt;/strong&gt; the &lt;strong&gt;maintenance&lt;/strong&gt; of either xml files or bootstrapper code. &lt;/p&gt;  &lt;p&gt;this type of &lt;strong&gt;discovery made easy&lt;/strong&gt; in MEF 2.0.&lt;/p&gt;  &lt;p&gt;the following code is loading any type which is ending with a &amp;#39;Plugin&amp;#39; extension into the composition.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ff2c9a86-b3b7-4515-ba59-2fcf9c74ec40" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;picker.ForTypesMatching(pl =&amp;gt; pl.Name.EndsWith(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Plugin&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;))&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.ExportInterfaces();&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;as you can see that the code is exporting all of the interfaces that was implemented by the plugins.&lt;/p&gt;  &lt;p&gt;foe example, if you are having the following interfaces and implementation:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:360fd12c-9d36-4033-a2af-e6d9cee2841a" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;interface&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ILogPlugin&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Write(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;string&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; message);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;LoggerPlugin&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ILogPlugin&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Write(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;string&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; message) { &lt;/span&gt;&lt;span style="background:#ffffff;color:#008000;"&gt;/* ... */}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;interface&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ISavePlugin&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Save(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;string&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; message);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;FilePlugin&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ISavePlugin&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Save(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;string&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; message) { &lt;/span&gt;&lt;span style="background:#ffffff;color:#008000;"&gt;/* ... */}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;LoggerPlugin will be export as ILogPlugin and FilePlugin will be export as ISavePlugin.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;ExportInterfaces&lt;/strong&gt; is having a few overloads which you can use to &lt;strong&gt;refine&lt;/strong&gt; the &lt;strong&gt;interface exporting&lt;/strong&gt;, instead of exporting all of the implemented interfaces, you can &lt;strong&gt;define a convention&lt;/strong&gt;, for example:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e1361df3-6c23-447e-bc96-327438266d41" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;picker.ForTypesMatching(pl =&amp;gt; pl.Name.EndsWith(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Plugin&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;))&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.ExportInterfaces(t =&amp;gt; t.Name.EndsWith(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Plugin&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;));&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;&lt;strong&gt;MEF 2.0 fluent&lt;/strong&gt; and &lt;strong&gt;conventional model&lt;/strong&gt; is a great improvement over the Attribute model. as you can see you can use both model together, but the convention model can ease your maintenance.&lt;/p&gt;  &lt;p&gt;in the next post I will discuss it further. &lt;/p&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/MEF-20-mini-series-part-2-Fluent-and-Conventional-Export-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2013%2F01%2F06%2Fmef-2-0-mini-series-part-2-fluent-and-conventional-export.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1634570" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/CompositionContainer/default.aspx">CompositionContainer</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/RegistrationBuilder/default.aspx">RegistrationBuilder</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Catalog/default.aspx">Catalog</category></item><item><title>MEF 2.0 TOC</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/06/mef-2-0-toc.aspx</link><pubDate>Sun, 06 Jan 2013 14:30:49 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1634514</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;MEF 2.0 TOC&lt;/h2&gt;  &lt;p&gt;&lt;strong&gt;MEF 2.0&lt;/strong&gt; is a reflection of a community requests.     &lt;br /&gt;features like &lt;strong&gt;&lt;em&gt;Open Generics&lt;/em&gt;&lt;/strong&gt;, &lt;em&gt;&lt;strong&gt;fluent&lt;/strong&gt; and &lt;strong&gt;conventional discovery&lt;/strong&gt;&lt;/em&gt;, &lt;em&gt;&lt;strong&gt;lifetime handling&lt;/strong&gt; of the part&lt;/em&gt;, &lt;strong&gt;&lt;em&gt;better exception handling &lt;/em&gt;&lt;/strong&gt;and more, was all requested by the community.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_8436_0F867DDB.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="MEF, Import, Export, CompositionContainer, RegistrationBuilder, Catalog, Generics" border="0" alt="MEF, Import, Export, CompositionContainer, RegistrationBuilder, Catalog, Generics" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_8436_thumb_46805C14.jpg" width="501" height="359" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/04/mef-2-0-mini-series-part-1.aspx" target="_blank"&gt;part 1: Open Generics&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/06/mef-2-0-mini-series-part-2-fluent-and-conventional-export.aspx" target="_blank"&gt;Part 2: Fluent and Conventional Export&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/09/mef-2-0-mini-series-part-3-fluent-import-constructor.aspx" target="_blank"&gt;Part 3: Fluent import constructor&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/12/mef-2-0-mini-series-part-4-fluent-import.aspx" target="_blank"&gt;Part 4: Fluent import&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/15/mef-2-0-mini-series-part-5-fluent-export-properties.aspx" target="_blank"&gt;Part 5: Fluent export properties.&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/16/mef-2-0-mini-series-part-6-composition-scoping-and-lifetime-management.aspx" target="_blank"&gt;Part 6: Composition scoping and lifetime management&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/24/mef-2-0-mini-series-part-7-catalog-filter-and-deep-hierarchic-scoping.aspx" target="_blank"&gt;Part 7: Catalog filter and Deep hierarchic scoping.&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/02/16/mef-2-0-mini-series-part-8-composition-options-and-exception-handling.aspx"&gt;Part 8: Composition options and exception handling&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;you can read more about MEF 1 series:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2009/11/28/mef-for-beginner-concept-part-1.aspx"&gt;Concept&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2009/12/05/mef-for-beginner-part-2-vs-2010.aspx"&gt;How To build your first MEF application&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2009/12/19/mef-for-beginner-part-3-hello-silverlight.aspx"&gt;Hello Silverlight&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2009/12/26/mef-for-beginner-part-4-a-sync-silverlight-loading.aspx"&gt;A-sync Silverlight loading&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/01/02/mef-for-beginner-part-5-import.aspx"&gt;Import&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/01/09/mef-for-beginner-part-creation-policy-part-6.aspx"&gt;Part Creation policy&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/01/15/mef-for-beginner-recomposition-policy-part-7.aspx"&gt;Recomposition policy&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/01/20/mef-for-beginner-metadata-part-8.aspx"&gt;Metadata (part 1)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/01/29/mef-for-beginner-repeatable-metadata-part-9.aspx"&gt;Metadata (part 2 – repeatable)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/02/13/mef-for-beginner-catalogs-part-10.aspx"&gt;Catalogs&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/03/20/mef-for-beginner-import-from-xaml-part-11.aspx"&gt;Import from Xaml&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/04/01/mef-for-beginner-deployment-catalog-part-12.aspx"&gt;Deployment Catalog&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/MEF-20-TOC-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2013%2F01%2F06%2Fmef-2-0-toc.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1634514" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Extensibility/default.aspx">Extensibility</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Composition/default.aspx">Composition</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ImportMany/default.aspx">ImportMany</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Generics/default.aspx">Generics</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/CompositionContainer/default.aspx">CompositionContainer</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/RegistrationBuilder/default.aspx">RegistrationBuilder</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Catalog/default.aspx">Catalog</category></item><item><title>MEF 2.0 - mini series: part 1 (Open Generics)</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/04/mef-2-0-mini-series-part-1.aspx</link><pubDate>Fri, 04 Jan 2013 08:33:21 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1626847</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;&lt;sub&gt;&lt;/sub&gt;MEF 2.0 - mini series: part 1 (Open Generics)&lt;/h2&gt;  &lt;p&gt;this is the first post of a new &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/06/mef-2-0-toc.aspx" target="_blank"&gt;mini series&lt;/a&gt; about &lt;strong&gt;MEF 2.0&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;MEF 2.0&lt;/strong&gt; is a reflection of a community requests.     &lt;br /&gt;features like &lt;strong&gt;&lt;em&gt;Open Generics&lt;/em&gt;&lt;/strong&gt;, &lt;em&gt;&lt;strong&gt;fluent&lt;/strong&gt; and &lt;strong&gt;conventional discovery&lt;/strong&gt;&lt;/em&gt;, &lt;em&gt;&lt;strong&gt;lifetime handling&lt;/strong&gt; of the part&lt;/em&gt;, &lt;strong&gt;&lt;em&gt;better exception handling &lt;/em&gt;&lt;/strong&gt;and more, was all requested by the community.&lt;/p&gt;  &lt;p&gt;each post of this mini series will target a single enhancement.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_6387_7380_66B4002B.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="MEF, Import, Export, CompositionContainer, RegistrationBuilder, Catalog, Generics" border="0" alt="MEF, Import, Export, CompositionContainer, RegistrationBuilder, Catalog, Generics" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_6387_7380_thumb_440FC1B0.jpg" width="524" height="430" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;h5&gt;Open Generics&lt;/h5&gt;  &lt;p&gt;one of the most annoying missing feature in MEF 1 was the lack of support for Open generics.&lt;/p&gt;  &lt;p&gt;the following code (which is fully functioning in MEF 2.0) didn&amp;#39;t worked in MEF 1.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:05f57ee5-6e06-469c-8255-46c7d06eef99" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;EventAggregator&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; IntAggregator { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }           &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;EventAggregator&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;T&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;event&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Action&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;T&amp;gt; Notify = (item) =&amp;gt; { };&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Send(T item)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;Notify(item);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;as obvious as it may seem, this piece of functionality is missing in MEF 1.    &lt;br /&gt;if you try to do it in MEF 1 it will look like the following code:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:824b6804-ae4f-453d-a312-102fdd438a9c" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;EventAggregatorInt&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; IntAggregator { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }           &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;EventAggregatorInt&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;EventAggregator&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; { }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;EventAggregator&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;T&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;event&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Action&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;T&amp;gt; Notify = (item) =&amp;gt; { };&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Send(T item)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;Notify(item);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;it is quite obvious that extra EventAggregatorInt class shouldn&amp;#39;t have be been there in the first place.&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;&lt;strong&gt;Open Generics&lt;/strong&gt; if fairly straight forward and it is keeping our code smaller and better.&amp;#160; &lt;/p&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/MEF-20-mini-series-part-1-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2013%2F01%2F04%2Fmef-2-0-mini-series-part-1.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1626847" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Generics/default.aspx">Generics</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/CompositionContainer/default.aspx">CompositionContainer</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/RegistrationBuilder/default.aspx">RegistrationBuilder</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Catalog/default.aspx">Catalog</category></item><item><title>MEF - Best Practice</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2011/07/27/mef-best-practice.aspx</link><pubDate>Thu, 28 Jul 2011 02:32:22 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:876925</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;MEF - Best Practice&lt;/h2&gt;  &lt;p&gt;In this post I will offer &lt;a href="http://mef.codeplex.com/" target="_blank"&gt;&lt;strong&gt;MEF&lt;/strong&gt;&lt;/a&gt;&amp;#160;&lt;strong&gt;practice&lt;/strong&gt; that may&lt;strong&gt; ease your MEF life&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;one of the &lt;strong&gt;MEF&lt;/strong&gt; &lt;strong&gt;headache&lt;/strong&gt; is to understand what&amp;#39;s goes wrong, &lt;/p&gt;  &lt;p&gt;when &lt;strong&gt;MEF &lt;/strong&gt;&lt;strong&gt;reject the composition&lt;/strong&gt;, you can read more about &lt;/p&gt;  &lt;p&gt;MEF diagnostic in &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2011/03/18/sdp-mef-real-life-patterns.aspx" target="_blank"&gt;this&lt;/a&gt; post.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/iStock_000005438250VerySmall_17C5917B.jpg"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="MEF, Import, Export, Import Many, Best practice" border="0" alt="MEF, Import, Export, Import Many, Best practice" src="http://blogs.microsoft.co.il/blogs/bnaya/iStock_000005438250VerySmall_thumb_563AB954.jpg" width="240" height="121" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Most of the obscure loading failure happens when your &lt;strong&gt;Export dependencies&lt;/strong&gt; &lt;strong&gt;doesn&amp;#39;t &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;satisfied&lt;/strong&gt;. in those case you solely relay on the MEF error message which is not enough &lt;/p&gt;  &lt;p&gt;in cases of deep dependencies graph.&lt;/p&gt;  &lt;p&gt;It will normally occurs when you are using the &lt;strong&gt;Import&lt;/strong&gt; attribute &lt;font color="#808080"&gt;(not ImportMany)&lt;/font&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;let take the following test case:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:78b28f31-127f-4d43-a90a-acef23661a6c" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IFoo&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Write();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;IFoo&lt;/span&gt;))]&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Foo&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;IFoo&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{ &lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    [&lt;span style="color:#2b91af;"&gt;Import&lt;/span&gt;]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IFoo&lt;/span&gt; Foo { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;class Bar importing IFoo, which mean that the composition will fail whenever&lt;/p&gt;  &lt;p&gt;MEF find no valid IFoo export or find more than single IFoo export.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;we can use 2 patterns:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;[ImportMany]&lt;/strong&gt; which will handle both the case of no export and too many export. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;[Import(AllowDefault=true)] &lt;/strong&gt;which cover the case of no export.       &lt;br /&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;using &lt;strong&gt;ImportMany&lt;/strong&gt; and the &lt;strong&gt;IPartImportsSatisfiedNotification&lt;/strong&gt; pattern.&lt;/p&gt;  &lt;p&gt;we can &lt;strong&gt;validate the composition&lt;/strong&gt; right after the composition stage.     &lt;br /&gt;&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b3b0a025-255b-4825-8e61-88075dad9419" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;IPartImportsSatisfiedNotification&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IFoo&lt;/span&gt; Foo { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    [&lt;span style="color:#2b91af;"&gt;ImportMany&lt;/span&gt;]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IFoo&lt;/span&gt;[] FoosInternal { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; OnImportsSatisfied()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (FoosInternal == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt; || FoosInternal.Length == 0)&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Exception&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;MEF composition find no IFoo&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (FoosInternal.Length &amp;gt; 1)&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Exception&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;MEF composition find to many IFoo&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;at the OnImportsSatisfied method you can check whether &lt;/p&gt;  &lt;p&gt;you got the composition right &lt;font color="#808080"&gt;(instead of letting the MEF infrastructure &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#808080"&gt;doing this job)&lt;/font&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;in most cases it is enough to ignore the to many IFoo composition case&lt;/p&gt;  &lt;p&gt;and to apply the &lt;strong&gt;[Import(AllowDefault=true)] &lt;/strong&gt;pattern.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:de4c27f3-7f68-4618-99bb-4260c31496e9" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;IPartImportsSatisfiedNotification&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    [&lt;span style="color:#2b91af;"&gt;Import&lt;/span&gt;(AllowDefault=&lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IFoo&lt;/span&gt; Foo { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; OnImportsSatisfied()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (Foo == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Exception&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;MEF composition find no IFoo&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;after the composition we can validate whether IFoo composition goes right.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;MEF failure at the composition phase can be frustrating.&lt;/p&gt;  &lt;p&gt;in this post I have tried to offer pattern to ease that pain.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;a href="http://dotnetshoutout.com/MEF-Best-Practice-Export-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2011%2F07%2F27%2Fmef-best-practice.aspx" /&gt;&lt;/a&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2011%2f07%2f27%2fmef-best-practice.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;div class="wlWriterHeaderFooter" style="text-align:right;margin:0px;padding:4px 0px 4px 0px;"&gt;&lt;a href="http://digg.com/submit?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2011%2f07%2f27%2fmef-best-practice.aspx&amp;amp;title=MEF+-+Best+Practice"&gt;&lt;img src="http://digg.com/img/badges/100x20-digg-button.png" width="100" height="20" alt="Digg This" title="Digg This" border="0" style="border:0;" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=876925" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Extensibility/default.aspx">Extensibility</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ImportMany/default.aspx">ImportMany</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Debug/default.aspx">Debug</category></item><item><title>SDP - MEF (Real-Life patterns)</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2011/03/18/sdp-mef-real-life-patterns.aspx</link><pubDate>Fri, 18 Mar 2011 13:36:51 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:808581</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;SDP - MEF (Real-Life patterns)&lt;/h2&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/image_6FD6A3D0.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="MEF, Compose, Import, ImportMany, Import, .net" border="0" alt="MEF, Compose, Import, ImportMany, Import, .net" align="left" src="http://blogs.microsoft.co.il/blogs/bnaya/image_thumb_5C85C764.png" width="63" height="84" /&gt;&lt;/a&gt;On the last day of the SDP at Crown Plaza Hotel, Tel Aviv.&lt;/p&gt;  &lt;p&gt;I was lecturing about &lt;strong&gt;Real-Life&lt;/strong&gt; scenario of &lt;a href="http://mef.codeplex.com/" target="_blank"&gt;&lt;strong&gt;MEF&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt; failures&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;the lecture was focusing on &lt;strong&gt;diagnostic&lt;/strong&gt; ,&lt;strong&gt;solutions&lt;/strong&gt; and &lt;strong&gt;testing&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;I want to thanks all attendant&amp;#39;s, It was the after lunch session and &lt;/p&gt;  &lt;p&gt;you were bravely kept your eyes open.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;the presentation &lt;a href="http://cid-9bf7c1a515d76a9a.office.live.com/browse.aspx/Sela%20public/SDP%202011" target="_blank"&gt;pdf&lt;/a&gt; can be found &lt;a href="http://cid-9bf7c1a515d76a9a.office.live.com/browse.aspx/Sela%20public/SDP%202011" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;you can use the following check-list as &lt;strong&gt;recommended diagnostic process&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Diagnostic Check-List &lt;/h5&gt;  &lt;ul&gt;   &lt;li&gt;Is the instance included in the composition &lt;/li&gt;    &lt;li&gt;Are the plug-in assemblies included in the deployment path &lt;/li&gt;    &lt;li&gt;Are the plug-in dependencies included in the probing path &lt;/li&gt;    &lt;li&gt;Is the Silverlight XAP included in the application &lt;/li&gt;    &lt;li&gt;Use Strong Name and versioning policy &lt;/li&gt;    &lt;li&gt;Do not refer to imported properties during construction time, use IPartImportsSatisfiedNotification &lt;/li&gt;    &lt;li&gt;Check that IEnumerable&amp;lt;T&amp;gt;’s are decorated with [ImportMany] as long as they come from multiple exports. &lt;/li&gt;    &lt;li&gt;Remember that errors in nested dependencies are sometimes obscured &lt;/li&gt;    &lt;li&gt;Use AllowDefault=true when nulls are permitted &lt;/li&gt;    &lt;li&gt;Use ComposeExportedValue for including mocks of your dependencies &lt;/li&gt; &lt;/ul&gt;&lt;div class="wlWriterHeaderFooter" style="text-align:right;margin:0px;padding:4px 0px 4px 0px;"&gt;&lt;a href="http://digg.com/submit?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2011%2f03%2f18%2fsdp-mef-real-life-patterns.aspx&amp;amp;title=SDP+-+MEF+(Real-Life+patterns)"&gt;&lt;img src="http://digg.com/img/badges/100x20-digg-button.png" width="100" height="20" alt="Digg This" title="Digg This" border="0" style="border:0;" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=808581" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Extensibility/default.aspx">Extensibility</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Composition/default.aspx">Composition</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ImportMany/default.aspx">ImportMany</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/VS2010/default.aspx">VS2010</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Debug/default.aspx">Debug</category></item><item><title>Dynamic MEF isn't thread safe by nature</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/12/04/dynamic-mef-isn-t-thread-safe-by-nature.aspx</link><pubDate>Sat, 04 Dec 2010 17:05:54 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:752599</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;Dynamic MEF isn&amp;#39;t thread safe by nature&lt;/h2&gt;  &lt;p&gt;this post focus a pitfall which you should be aware in order to avoid sporadic crashes.&lt;/p&gt;  &lt;p&gt;In general most of &lt;a href="http://mef.codeplex.com/" target="_blank"&gt;&lt;strong&gt;MEF&lt;/strong&gt;&lt;/a&gt; operation are operate once at the application initialization stage,&lt;/p&gt;  &lt;p&gt;and it normally happens on a single thread.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;this may not be true when you need to dynamically load &lt;strong&gt;MEF&lt;/strong&gt; parts (using &lt;strong&gt;GetExportedValue&lt;/strong&gt; or &lt;strong&gt;SatisfyImportsOnce&lt;/strong&gt;).&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;unfortunately &lt;strong&gt;MEF&lt;/strong&gt; underline is using &lt;strong&gt;non concurrent dictionary&lt;/strong&gt; which is &lt;strong&gt;not thread safe&lt;/strong&gt; for the above operation implementation.&lt;/p&gt;  &lt;p&gt;therefore the following code will &lt;strong&gt;crash sporadically&lt;/strong&gt;.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:456d762d-6216-4361-b71f-e55bace18916" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;] &lt;span style="color:#008000;"&gt;// exported type which will instantiate using MEF&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ExportedType&lt;/span&gt; {}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Program&lt;/span&gt;&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Main(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;[] args)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; catalog = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;AssemblyCatalog&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;Program&lt;/span&gt;).Assembly);&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; container = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;CompositionContainer&lt;/span&gt;(catalog);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 100; i++)&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Thread&lt;/span&gt;(() =&amp;gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                {   &lt;span style="color:#008000;"&gt;// get the exported part dynamically (instantiate by MEF)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                    container.GetExportedValue&amp;lt;&lt;span style="color:#2b91af;"&gt;ExportedType&lt;/span&gt;&amp;gt;();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                }).Start();&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;line 15: &lt;strong&gt;GetExportedValue&lt;/strong&gt; call is a &lt;strong&gt;non protected critical section&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;whenever using &lt;strong&gt;dynamic MEF&lt;/strong&gt; techniques you should put a &lt;strong&gt;critical section synchronization&lt;/strong&gt; over it.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;the code sample can be found &lt;a href="http://cid-9bf7c1a515d76a9a.office.live.com/browse.aspx/Code%20Samples/MEF/Thread%20safe?uc=1" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Credits&lt;/h5&gt;  &lt;p&gt;&lt;strong&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/stiller/" target="_blank"&gt;Eran Stiller&lt;/a&gt;&lt;/strong&gt; was the first one who&amp;#39;s point my intention to this issue.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2010%2f12%2f04%2fdynamic-mef-isn-t-thread-safe-by-nature.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt; &lt;a href="http://dotnetshoutout.com/Dynamic-MEF-isnt-thread-safe-by-nature-Export-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2010%2F12%2F04%2Fdynamic-mef-isn-t-thread-safe-by-nature.aspx" /&gt;&lt;/a&gt;&lt;div class="wlWriterHeaderFooter" style="text-align:right;margin:0px;padding:4px 0px 4px 0px;"&gt;&lt;a href="http://digg.com/submit?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2010%2f12%2f04%2fdynamic-mef-isn-t-thread-safe-by-nature.aspx&amp;amp;title=Dynamic+MEF+isn&amp;#39;t+thread+safe+by+nature"&gt;&lt;img src="http://digg.com/img/badges/100x20-digg-button.png" width="100" height="20" alt="Digg This" title="Digg This" border="0" style="border:0;" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=752599" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Extensibility/default.aspx">Extensibility</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/expert/default.aspx">expert</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category></item><item><title>Exporting non Exportable types</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/10/30/exporting-non-exportable-types.aspx</link><pubDate>Sat, 30 Oct 2010 19:23:56 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:734376</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;Exporting non Exportable types&lt;/h2&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/1288445311_Startup_Wizard_7DC77FDF.jpg"&gt;&lt;img style="border-right-width:0px;margin:0px 10px 0px 0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="MEF, Compose, Import, Export, Poco" border="0" alt="MEF, Compose, Import, Export, Poco" align="left" src="http://blogs.microsoft.co.il/blogs/bnaya/1288445311_Startup_Wizard_thumb_6BBB3C52.jpg" width="65" height="65" /&gt;&lt;/a&gt; this post extend &lt;strong&gt;Glenn Block&lt;/strong&gt;&amp;#39;s post about &lt;/p&gt;  &lt;p&gt;&amp;quot;&lt;a href="http://codebetter.com/blogs/glenn.block/archive/2009/04/27/poco-mef-and-custom-type-systems-are-you-ready-to-take-the-red-pill.aspx" target="_blank"&gt;Poco, Mef, and custom type systems. Are you ready to take the red pill?&amp;quot;&lt;/a&gt;&amp;#160; &lt;/p&gt;  &lt;p&gt;the post is adding &lt;font color="#000000"&gt;a &lt;strong&gt;compile time attribute export model&lt;/strong&gt;, &lt;strong&gt;Directory catalog&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#000000"&gt;and migrate Glenn code to &lt;strong&gt;VS 2010&lt;/strong&gt;&lt;/font&gt;.&lt;/p&gt;  &lt;p&gt;It is very recommended to read &lt;strong&gt;Glenn Block&lt;/strong&gt;&amp;#39;s &lt;a href="http://codebetter.com/blogs/glenn.block/archive/2009/04/27/poco-mef-and-custom-type-systems-are-you-ready-to-take-the-red-pill.aspx" target="_blank"&gt;post&lt;/a&gt; before reading &lt;/p&gt;  &lt;p&gt;this one.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;the &lt;strong&gt;code sample&lt;/strong&gt; for this post can be download from &lt;a href="http://cid-9bf7c1a515d76a9a.office.live.com/browse.aspx/Code%20Samples/MEF/TypeExtensions" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Summarizing Glenn&amp;#39;s post&lt;/h5&gt;  &lt;p&gt;in general Glenn show how to add attributes (like Export) to 3rd party type&lt;/p&gt;  &lt;p&gt;which doesn&amp;#39;t support it (&lt;em&gt;&lt;font color="#808080"&gt;it is cool technique which doesn&amp;#39;t related directly to &lt;/font&gt;&lt;/em&gt;&lt;a href="http://mef.codeplex.com/" target="_blank"&gt;&lt;em&gt;&lt;font color="#808080"&gt;MEF&lt;/font&gt;&lt;/em&gt;&lt;/a&gt;).&lt;/p&gt;  &lt;p&gt;Glenn show how to write catalog which can be aware of types extension.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;What was added?&lt;/h5&gt;  &lt;p&gt;I took Glenn&amp;#39;s source, fix it to run against the CLR 4.0,&lt;/p&gt;  &lt;p&gt;then I added a &lt;strong&gt;directory catalog&lt;/strong&gt; which aware of the &lt;/p&gt;  &lt;p&gt;type extension and I also add a nice clean way of exposing the type &lt;/p&gt;  &lt;p&gt;by using &lt;strong&gt;assembly level attribute&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;font color="#808080"&gt;the directory catalog is very straight forward so I wont cover it in the post &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#808080"&gt;(you can download the &lt;/font&gt;&lt;a href="http://cid-9bf7c1a515d76a9a.office.live.com/browse.aspx/Code%20Samples/MEF/TypeExtensions" target="_blank"&gt;&lt;font color="#808080"&gt;source&lt;/font&gt;&lt;/a&gt;&lt;font color="#808080"&gt; and see the implementation).&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;the following code show how to use the assembly level attribute:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e8842f66-b6a0-430a-927c-44d416a8d343" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;"&gt; &lt;li&gt;[&lt;span style="color:#0000ff;"&gt;assembly&lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;ComposablePartRegistry&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;MyNoneExportedType&lt;/span&gt;), &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;IMyNoneExportedType&lt;/span&gt;))]&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;as you can see, all you have to do is to declare the &lt;strong&gt;exported type&lt;/strong&gt; followed by the &lt;strong&gt;exported contract&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;the attribute snippet:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f7f9f1e4-2e8d-401d-a5d9-6730fedd2073" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;[&lt;span style="color:#2b91af;"&gt;AttributeUsage&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;AttributeTargets&lt;/span&gt;.Assembly)]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ComposablePartRegistryAttribute&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;Attribute&lt;/span&gt;&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ComposablePartRegistryAttribute(&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af;"&gt;Type&lt;/span&gt; implementation, &lt;span style="color:#2b91af;"&gt;Type&lt;/span&gt; contract, &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#2b91af;"&gt;CreationPolicy&lt;/span&gt; policy = &lt;span style="color:#2b91af;"&gt;CreationPolicy&lt;/span&gt;.Any)&lt;/li&gt; &lt;li&gt;    { &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#2b91af;"&gt;Type&lt;/span&gt; t = &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;ComposablePartType&lt;/span&gt;&amp;lt;,&amp;gt;);&lt;/li&gt; &lt;li&gt;        t = t.MakeGenericType(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;[] {implementation, contract});&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        Type = &lt;span style="color:#2b91af;"&gt;Activator&lt;/span&gt;.CreateInstance(t, policy) &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;TypeDelegator&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;TypeDelegator&lt;/span&gt; Type { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;line 8-10: creating &lt;strong&gt;ComposablePartType&lt;/strong&gt; type which later be used by the &lt;strong&gt;catalog&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;this was small extension to Glenn&amp;#39;s concept which can be very useful &lt;/p&gt;  &lt;p&gt;in cases where you want to embrace 3rs party component into your &lt;strong&gt;MEF&lt;/strong&gt; model.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2010%2f10%2f30%2fexporting-non-exportable-types.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt; &lt;a href="http://dotnetshoutout.com/Exporting-non-Exportable-types-Export-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2010%2F10%2F30%2Fexporting-non-exportable-types.aspx" /&gt;&lt;/a&gt;&lt;div class="wlWriterHeaderFooter" style="text-align:right;margin:0px;padding:4px 0px 4px 0px;"&gt;&lt;a href="http://digg.com/submit?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2010%2f10%2f30%2fexporting-non-exportable-types.aspx&amp;amp;title=Exporting+non+Exportable+types"&gt;&lt;img src="http://digg.com/img/badges/100x20-digg-button.png" width="100" height="20" alt="Digg This" title="Digg This" border="0" style="border:0;" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=734376" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Extensibility/default.aspx">Extensibility</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Composition/default.aspx">Composition</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ImportMany/default.aspx">ImportMany</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Poco/default.aspx">Poco</category></item><item><title>Testing and Debugging MEF, avoiding misconceptions - Part 3</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/10/29/testing-and-debugging-mef-avoiding-misconceptions-part-3.aspx</link><pubDate>Fri, 29 Oct 2010 13:34:32 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:733620</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;Testing and Debugging &lt;strong&gt;MEF&lt;/strong&gt;, avoiding misconceptions - Part 3&lt;/h2&gt;  &lt;p&gt;&lt;img style="margin:0px 10px 0px 0px;display:inline;" title="MEf, Import, Export, Compose, extension, extensibility" alt="MEf, Import, Export, Compose, extension, extensibility" align="left" src="http://officeimg.vo.msecnd.net/en-us/images/MH900078811.jpg" width="105" height="105" /&gt;this is the 3rd post of this series and it will &lt;/p&gt;  &lt;p&gt;discuss common &lt;strong&gt;misconceptions&lt;/strong&gt; which may lead unexpected behavior and long &lt;/p&gt;  &lt;p&gt;debugging nights.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;&lt;font color="#000080"&gt;Initialization issues&lt;/font&gt;&lt;/h5&gt;  &lt;p&gt;the first misconception occurs when developer are trying to access&lt;/p&gt;  &lt;p&gt;imported property at construction time.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f21a87e1-2e3d-498c-a2db-bede96e4a40f" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;NullReferenceException&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyPlugin&lt;/span&gt;&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; MyPlugin()&lt;/li&gt; &lt;li&gt;    {   &lt;span style="color:#008000;"&gt;// the logger import doesn&amp;#39;t satisfied yet &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#008000;"&gt;// the next line will result with NullReferenceException&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        Logger.Write(&lt;span style="color:#a31515;"&gt;&amp;quot;MyPlugin initialized&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    [&lt;span style="color:#2b91af;"&gt;Import&lt;/span&gt;]&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt; Logger { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;MyPlugin instantiation occur when the &lt;a href="http://mef.codeplex.com/" target="_blank"&gt;&lt;strong&gt;MEF&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt; container composed&lt;/strong&gt;,&lt;/p&gt;  &lt;p&gt;the composition process will create an instance and only then it will try&lt;/p&gt;  &lt;p&gt;to &lt;strong&gt;satisfy&lt;/strong&gt; its &lt;strong&gt;imports&lt;/strong&gt;. which mean that at the construction time the &lt;strong&gt;imports&lt;/strong&gt; does not yet satisfied.&lt;/p&gt;  &lt;p&gt;the above code will result with &lt;strong&gt;NullReferenceException&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h6&gt;What can we do about it?&lt;/h6&gt;  &lt;p&gt;we can have a few solutions solving this issue:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;we can use &lt;strong&gt;ImportingConstructor&lt;/strong&gt; (&lt;font color="#808080"&gt;&lt;em&gt;but we should use it carefully because          &lt;br /&gt;&lt;strong&gt;cross import constructor&lt;/strong&gt; may lead to &lt;strong&gt;deadlock&lt;/strong&gt; and &lt;strong&gt;rejection&lt;/strong&gt;&lt;/em&gt;&lt;/font&gt;). &lt;/li&gt; &lt;/ul&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:69aeccd1-e1fc-4308-8b6a-6e58be29c4b2" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;ImportingConstructor&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyPlugin&lt;/span&gt;&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#008000;"&gt;// cross import constructor can lead to deadlock an rejection&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    [&lt;span style="color:#2b91af;"&gt;ImportingConstructor&lt;/span&gt;] &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; MyPlugin(&lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt; logger)&lt;/li&gt; &lt;li&gt;    {  &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        logger.Write(&lt;span style="color:#a31515;"&gt;&amp;quot;MyPlugin initialized&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;this way the logger will be instantiate before the instantiation of MyPlugin.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;we can implement &lt;strong&gt;IPartImportsSatisfiedNotification &lt;/strong&gt;(&lt;em&gt;&lt;font color="#808080"&gt;which is my favorite technique because there is no dead lock risk&lt;/font&gt;&lt;/em&gt;).       &lt;br /&gt;this way we can &lt;strong&gt;initialize&lt;/strong&gt; our component right &lt;strong&gt;after all import&lt;/strong&gt;s has been &lt;strong&gt;satisfied&lt;/strong&gt;. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:da6b093e-de9e-46aa-9342-aef587dc0eb8" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;IPartImportsSatisfiedNotificat&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyPlugin&lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;IPartImportsSatisfiedNotification&lt;/span&gt; &lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; OnImportsSatisfied()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#008000;"&gt;// happens just after the imports satisfaction&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        Logger.Write(&lt;span style="color:#a31515;"&gt;&amp;quot;MyPlugin initialized&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    [&lt;span style="color:#2b91af;"&gt;Import&lt;/span&gt;]&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt; Logger { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;MEF infrastructure will call &lt;strong&gt;OnImportsSatisfied&lt;/strong&gt; right after it done with the composition process. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;&lt;font color="#000080"&gt;None composed instance issue&lt;/font&gt;&lt;/h5&gt;  &lt;p&gt;developers are used to instantiate their own type using the new keyword (&lt;em&gt;&lt;font color="#808080"&gt;var plugin = new MyPlugin()&lt;/font&gt;&lt;/em&gt;).&lt;/p&gt;  &lt;p&gt;doing so &lt;strong&gt;wouldn&amp;#39;t satisfy&lt;/strong&gt; any of the &lt;strong&gt;imported&lt;/strong&gt; properties (&lt;em&gt;&lt;font color="#808080"&gt;because the type doesn&amp;#39;t took part in any composition&lt;/font&gt;&lt;/em&gt;), because MEF does not aware of the instantiation.&lt;/p&gt;  &lt;h6&gt;How can we do it right?&lt;/h6&gt;  &lt;ul&gt;   &lt;li&gt;we can introduce the instance to the &lt;strong&gt;composition&lt;/strong&gt; by using &lt;strong&gt;ComposePart&lt;/strong&gt;.       &lt;br /&gt;the compose part will &lt;strong&gt;satisfy&lt;/strong&gt; any of the instance &lt;strong&gt;imports&lt;/strong&gt;. &lt;/li&gt; &lt;/ul&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:21f12783-aa55-4248-ac51-a17412df34bd" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;ComposeParts&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;[&lt;span style="color:#2b91af;"&gt;TestMethod&lt;/span&gt;]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; GetInstanceFromContainerTest()&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; catalog = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;AssemblyCatalog&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt;).Assembly);&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; container = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;CompositionContainer&lt;/span&gt;(catalog);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; plugin = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyPlugin&lt;/span&gt;(); ;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    container.ComposeParts(plugin);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#2b91af;"&gt;Assert&lt;/span&gt;.IsNotNull(plugin.Logger);&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Write(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; content);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt;))]&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyLogger&lt;/span&gt;:&lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Write(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; content)&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af;"&gt;Debug&lt;/span&gt;.WriteLine(content);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyPlugin&lt;/span&gt; &lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    [&lt;span style="color:#2b91af;"&gt;Import&lt;/span&gt;]&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt; Logger { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;ul&gt;   &lt;li&gt;other solution is getting our instance dynamically from      &lt;br /&gt;the container (&lt;em&gt;&lt;font color="#808080"&gt;for example by using &lt;strong&gt;GetExportedValue&lt;/strong&gt;&lt;/font&gt;&lt;/em&gt;) &lt;/li&gt; &lt;/ul&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c12cb299-75f0-4eff-b63e-6366dc1e7af6" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;GetExportedValue&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;[&lt;span style="color:#2b91af;"&gt;TestMethod&lt;/span&gt;]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; GetInstanceFromContainerTest()&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; catalog = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;AssemblyCatalog&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt;).Assembly);&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; container = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;CompositionContainer&lt;/span&gt;(catalog);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    container.Compose(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;CompositionBatch&lt;/span&gt;());&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#2b91af;"&gt;MyPlugin&lt;/span&gt; plugin = container.GetExportedValue&amp;lt;&lt;span style="color:#2b91af;"&gt;MyPlugin&lt;/span&gt;&amp;gt;();&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#2b91af;"&gt;Assert&lt;/span&gt;.IsNotNull(plugin);&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af;"&gt;Assert&lt;/span&gt;.IsNotNull(plugin.Logger);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt;&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Write(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; content);&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt;))]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyLogger&lt;/span&gt;:&lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt;&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Write(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; content)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#2b91af;"&gt;Debug&lt;/span&gt;.WriteLine(content);&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;]&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyPlugin&lt;/span&gt; &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    [&lt;span style="color:#2b91af;"&gt;Import&lt;/span&gt;]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt; Logger { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;ul&gt;   &lt;li&gt;using &lt;strong&gt;Silverlight&lt;/strong&gt; we can initialize our container       &lt;br /&gt;by using &lt;strong&gt;CompositionHost.Initialize(catalog)&lt;/strong&gt;. and we can add the       &lt;br /&gt;following line to the constructor:       &lt;br /&gt;&lt;strong&gt;CompositionInitializer.SatisfyImports(this)&lt;/strong&gt;,&lt;strong&gt; &lt;/strong&gt;which mean that       &lt;br /&gt;each time we are creating new instance it will try to &lt;strong&gt;satisfy&lt;/strong&gt; it&amp;#39;s own &lt;strong&gt;imports&lt;/strong&gt;.       &lt;br /&gt;(&lt;font color="#808080"&gt;be aware that you must use &lt;strong&gt;CompositionHost.Initialize(catalog)&lt;/strong&gt; before using         &lt;br /&gt;&lt;/font&gt;&lt;strong&gt;&lt;font color="#808080"&gt;CompositionInitializer.SatisfyImports(this)&lt;/font&gt;&lt;/strong&gt;) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c3f18a4b-580c-4b31-a9b5-c66178ad05d6" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;SatisfyImports&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyPlugin&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; MyPlugin()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af;"&gt;CompositionInitializer&lt;/span&gt;.SatisfyImports(&lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    [&lt;span style="color:#2b91af;"&gt;Import&lt;/span&gt;]&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt; Logger { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;&lt;font color="#404040"&gt;if you want to use the same technique for none Silverlight components,&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;&lt;font color="#404040"&gt;you can download the code for System.ComponentModel.Composition.Initialization.dll&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;&lt;font color="#404040"&gt;which hold &lt;strong&gt;CompositionHost.Initialize&lt;/strong&gt; and &lt;strong&gt;CompositionInitializer.SatisfyImports&lt;/strong&gt;&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;&lt;font color="#404040"&gt;from &lt;a href="http://cid-9bf7c1a515d76a9a.office.live.com/browse.aspx/Code%20Samples/MEF/Initialization/2010" target="_blank"&gt;here&lt;/a&gt;.&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;&lt;font color="#404040"&gt;&lt;/font&gt;&lt;/em&gt;&lt;/p&gt; &lt;font color="#000080"&gt;&lt;/font&gt;  &lt;h6&gt;&lt;font color="#000080"&gt;Exporting the wrong type issue&lt;/font&gt;&lt;/h6&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;last issue is to &lt;strong&gt;avoid exporting&lt;/strong&gt; or &lt;strong&gt;importing&lt;/strong&gt; the wrong type.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;MEF&lt;/strong&gt; does not goes through the &lt;strong&gt;inheritance&lt;/strong&gt; list, &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;MEF contract&lt;/strong&gt; are &lt;strong&gt;explicit &lt;/strong&gt;and doesn&amp;#39;t aware of the inheritance chain.&lt;/p&gt;  &lt;p&gt;the following code snippet export the MyLogger (&lt;em&gt;&lt;font color="#808080"&gt;not the ILogger&lt;/font&gt;&lt;/em&gt;).&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e9183357-7b3e-4ce3-bc76-a3d231c8c4c5" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;"&gt; &lt;li&gt;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyLogger&lt;/span&gt;:&lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt; {&lt;/li&gt; &lt;li&gt;    ...&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;it is equivalent to:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b58fcc81-9f14-4510-abe3-578609daf929" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;"&gt; &lt;li&gt;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;MyLogger&lt;/span&gt;))]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyLogger&lt;/span&gt;:&lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt; {&lt;/li&gt; &lt;li&gt;    ...&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;while what you may really wanted is:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:67558ecb-daed-4c73-ad5e-e92fb53de210" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;"&gt; &lt;li&gt;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt;))]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyLogger&lt;/span&gt;:&lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt; {&lt;/li&gt; &lt;li&gt;    ...&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;the following import won&amp;#39;t get the first 2 snippet because of the explicit contract comparison:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:38edf86b-6a31-418f-9114-b1ed9a1941e9" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;"&gt; &lt;li&gt;[&lt;span style="color:#2b91af;"&gt;Import&lt;/span&gt;]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt; Logger { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;&lt;strong&gt;MEF instantiation&lt;/strong&gt; occurs at runtime and sometime it is not &lt;/p&gt;  &lt;p&gt;so easy or straight forward for debug.&lt;/p&gt;  &lt;p&gt;avoiding common misconceptions may save us long debugging hours.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Read more&lt;/h5&gt;  &lt;p&gt;I strongly recommend the following post which discuss other points of failure:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a title="http://blogs.msdn.com/b/dsplaisted/archive/2010/07/13/how-to-debug-and-diagnose-mef-failures.aspx" href="http://blogs.msdn.com/b/dsplaisted/archive/2010/07/13/how-to-debug-and-diagnose-mef-failures.aspx"&gt;How to Debug and Diagnose MEF Failures&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&amp;#160;&lt;a href="http://mef.codeplex.com/wikipage?title=Debugging%20and%20Diagnostics" target="_blank"&gt;Diagnosing Composition Problems&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2010%2f10%2f29%2ftesting-and-debugging-mef-avoiding-misconceptions-part-3.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt; &lt;a href="http://dotnetshoutout.com/Testing-and-Debugging-MEF-avoiding-misconceptions-Part-3-Export-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2010%2F10%2F29%2Ftesting-and-debugging-mef-avoiding-misconceptions-part-3.aspx" /&gt;&lt;/a&gt;&lt;div class="wlWriterHeaderFooter" style="text-align:right;margin:0px;padding:4px 0px 4px 0px;"&gt;&lt;a href="http://digg.com/submit?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2010%2f10%2f29%2ftesting-and-debugging-mef-avoiding-misconceptions-part-3.aspx&amp;amp;title=Testing+and+Debugging+MEF%2c+avoiding+misconceptions+-+Part+3"&gt;&lt;img src="http://digg.com/img/badges/100x20-digg-button.png" width="100" height="20" alt="Digg This" title="Digg This" border="0" style="border:0;" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=733620" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Extensibility/default.aspx">Extensibility</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Composition/default.aspx">Composition</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ImportMany/default.aspx">ImportMany</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Debug/default.aspx">Debug</category></item></channel></rss>