<?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 : Composition</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Composition/default.aspx</link><description>Tags: Composition</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 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>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>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><item><title>Testing and Debugging MEF, Tips - Part 2</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/10/12/testing-and-debugging-mef-tips-part-2.aspx</link><pubDate>Wed, 13 Oct 2010 00:41:04 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:725150</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;Testing and Debugging MEF, Tips - Part 2&lt;/h2&gt;  &lt;p&gt;this is the second post of a series that will offer some tips &lt;/p&gt;  &lt;p&gt;about &lt;strong&gt;testing&lt;/strong&gt; and &lt;strong&gt;debugging&lt;/strong&gt; your &lt;a href="http://mef.codeplex.com/"&gt;&lt;strong&gt;MEF&lt;/strong&gt;&lt;/a&gt;-able component and application.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;in this post we will focus about &lt;strong&gt;debugging&lt;/strong&gt; the most common, and most confusing, &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;MEF failure&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Assembly Loading Failure&lt;/h5&gt;  &lt;p&gt;the most common &lt;strong&gt;MEF&lt;/strong&gt; runtime failure occurs because of missing assembly&lt;/p&gt;  &lt;p&gt;which contain the &lt;strong&gt;MEF parts&lt;/strong&gt; (Import, Export) or &lt;/p&gt;  &lt;p&gt;the &lt;strong&gt;parts dependencies&lt;/strong&gt; (like 3rd party components).&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;under the hood &lt;strong&gt;MEF&lt;/strong&gt; is using &lt;strong&gt;reflection&lt;/strong&gt;, which mean that MEF loading &lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;behavior is similar to the reflection dynamic loading.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Tracing Loading Failure?&lt;/h5&gt;  &lt;p&gt;I will suggest 2 option for tracing the failure:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;1. we can register to &lt;strong&gt;assembly resolve&lt;/strong&gt; event (&lt;em&gt;AppDomain.CurrentDomain.AssemblyResolve&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:79ba2bc3-9fa9-4326-a8e8-193c9ba3dee2" 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;AppDomain&lt;/span&gt;.CurrentDomain.AssemblyResolve += OnAssemblyResolveHandler;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Assembly&lt;/span&gt; OnAssemblyResolveHandler(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af;"&gt;ResolveEventArgs&lt;/span&gt; args)&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    Logger.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;Assembly Resolve: &amp;quot;&lt;/span&gt; + args.Name);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; args.RequestingAssembly;&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;the problem with that approach is that we may get some smoke events which &lt;/p&gt;  &lt;p&gt;actually succeed (because of the .NET probing).&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;2. catching &lt;strong&gt;ReflectionTypeLoadException&lt;/strong&gt; exception&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:7fd8a24f-7c0a-4a30-87c6-aa6edb0ec649" 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;try&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    container.ComposeParts(p);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;catch&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;ReflectionTypeLoadException&lt;/span&gt; ex)&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;Exception&lt;/span&gt; exc &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; ex.LoaderExceptions)&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    {&lt;/li&gt; &lt;li&gt;        Logger.WriteLine(exc.Message);&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;we can wrap the composition using try catch scope. &lt;/p&gt;  &lt;p&gt;and catch &lt;strong&gt;ReflectionTypeLoadException.&lt;/strong&gt; this will &lt;/p&gt;  &lt;p&gt;give us pretty good idea what&amp;#39;s got wrong.&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;my suggestion is whenever possible, wrapping any composition and catch the &lt;strong&gt;ReflectionTypeLoadException&lt;/strong&gt;, in other cases we can use the first technique.&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://sourceforge.net/projects/streamrecnet/" target="_blank"&gt;Ohad Schneider&lt;/a&gt;&lt;/strong&gt; should have credit for part of the code in this post. &lt;/p&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2010%2f10%2f12%2ftesting-and-debugging-mef-tips-part-2.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-Tips-Part-2-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%2F12%2Ftesting-and-debugging-mef-tips-part-2.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%2f12%2ftesting-and-debugging-mef-tips-part-2.aspx&amp;amp;title=Testing+and+Debugging+MEF%2c+Tips+-+Part+2"&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=725150" 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/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><item><title>Testing and Debugging MEF, Tips - Part 1</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/07/09/testing-and-debugging-mef-tips-part-1.aspx</link><pubDate>Fri, 09 Jul 2010 22:32:17 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:677606</guid><dc:creator>bnaya</dc:creator><slash:comments>1</slash:comments><description>&lt;h2&gt;Testing and Debugging MEF, Tips - Part 1&lt;/h2&gt;  &lt;p&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="Text, Debug, MEF" border="0" alt="Text, Debug, MEF" align="left" src="http://officeimg.vo.msecnd.net/en-us/images/MH900238955.jpg" width="152" height="152" /&gt;&lt;/p&gt;  &lt;p&gt;this is the first post of a series that will offer some tips &lt;/p&gt;  &lt;p&gt;about &lt;strong&gt;testing&lt;/strong&gt; and &lt;strong&gt;debugging&lt;/strong&gt; your &lt;a href="http://mef.codeplex.com/" target="_blank"&gt;&lt;strong&gt;MEF&lt;/strong&gt;&lt;/a&gt;-able component and application.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;in this post we will focus about exporting &lt;strong&gt;Mock&lt;/strong&gt; objects.&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 &lt;a href="http://cid-9bf7c1a515d76a9a.office.live.com/self.aspx/Code%20Samples/MEF/Test%20and%20Debug%20Tips/MockMEF.zip" target="_blank"&gt;&lt;strong&gt;download&lt;/strong&gt;&lt;/a&gt; from &lt;a href="http://cid-9bf7c1a515d76a9a.office.live.com/self.aspx/Code%20Samples/MEF/Test%20and%20Debug%20Tips/MockMEF.zip" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Prerequisite &lt;/h5&gt;  &lt;p&gt;if you are not familiar with &lt;strong&gt;Mocks &lt;/strong&gt;you better read more about this subject before&lt;/p&gt;  &lt;p&gt;proceeding with this post (in short &lt;strong&gt;Mocks&lt;/strong&gt; are fake object which is use for separate&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;dependencies&lt;/strong&gt; during &lt;strong&gt;unit testing&lt;/strong&gt;).&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;the sample is using the &lt;strong&gt;&lt;a href="http://code.google.com/p/moq/downloads/detail?name=Moq.4.0.10531.7-bin.zip&amp;amp;can=2&amp;amp;q=" target="_blank"&gt;Moq&lt;/a&gt;&lt;/strong&gt; library as it mocking infrastructure.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Having the following code:&lt;/h5&gt;  &lt;p&gt;1. contract&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:df79fdf0-205c-4cff-b41b-21d152603f92" 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;Contract&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;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; message);&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;&amp;#160;&lt;/p&gt;  &lt;p&gt;2.exported contract 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:c4e98389-eb49-4cfc-a5a3-86df5097ed67" 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;Export (contract implementor)&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;DebugLogger&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; message)&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(message);&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;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line1&lt;/strong&gt;: export the class as ILogger&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;3. contract consumer&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:ac0bb1e6-8fc4-43ee-ba78-a5aaa947bb8b" 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;Import (contract consumer)&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;MyLogic&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&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;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt;&amp;gt; Loggers { &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; DoYourThings()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt; logger &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; Loggers)&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;Some log&amp;quot;&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:#2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;Done&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;&lt;strong&gt;line 3&lt;/strong&gt;: import any available loggers&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;4. composition and execution&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:ed099f47-266a-4fd6-8ae8-4d84b643d45e" 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;Main&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;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Program&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&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 style="background:#f3f3f3;"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; logic = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyLogic&lt;/span&gt;();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; cat = &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;(cat);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        container.ComposeParts(logic);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        logic.DoYourThings();&lt;/li&gt; &lt;li&gt;        logic.DoYourThings();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.ReadKey();&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;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;How can we write unit test for the DoYourThings method?&lt;/h5&gt;  &lt;p&gt;&lt;strong&gt;MEF&lt;/strong&gt; dependencies is already loosely coupled by it nature, but in order to&lt;/p&gt;  &lt;p&gt;validate the method behavior we still need to check whether the the method&lt;/p&gt;  &lt;p&gt;do write logs.&lt;/p&gt;  &lt;p&gt;we will inject a &lt;strong&gt;Mock&amp;lt;ILogger&amp;gt;&lt;/strong&gt; into the &lt;strong&gt;MEF container&lt;/strong&gt; in order to validate &lt;/p&gt;  &lt;p&gt;this behavior.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h6&gt;the following code snippet is showing how:&lt;/h6&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:2ff2b45b-81f0-4049-914d-9a50abd075ca" 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;Test&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; TestMethod1()&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#008000;"&gt;// Arrange&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; logic = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyLogic&lt;/span&gt;();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&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;();&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; loggerMock = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Mock&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt;&amp;gt;();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    container.ComposeExportedValue&amp;lt;&lt;span style="color:#2b91af;"&gt;ILogger&lt;/span&gt;&amp;gt;(loggerMock.Object);&lt;/li&gt; &lt;li&gt;    container.ComposeParts(logic);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000;"&gt;// Test&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    logic.DoYourThings();&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#008000;"&gt;// Validate&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    loggerMock.Verify(logger =&amp;gt; logger.Write(&lt;span style="color:#2b91af;"&gt;It&lt;/span&gt;.IsAny&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&amp;gt;()), &lt;span style="color:#2b91af;"&gt;Times&lt;/span&gt;.Once());&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;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line 7&lt;/strong&gt;: &lt;strong&gt;creating&lt;/strong&gt; &lt;strong&gt;mock&lt;/strong&gt; object.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line 8&lt;/strong&gt;: &lt;strong&gt;inject&lt;/strong&gt; the &lt;strong&gt;mock&lt;/strong&gt; object into the container.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;using the &lt;strong&gt;ComposeExportedValue&lt;/strong&gt; method, we can inject&lt;/p&gt;  &lt;p&gt;mock object into our &lt;strong&gt;CompositionContainer&lt;/strong&gt;, therefore at the &lt;strong&gt;composition&lt;/strong&gt; stage&lt;/p&gt;  &lt;p&gt;the &lt;strong&gt;Import&lt;/strong&gt; properties will get our &lt;strong&gt;mock&lt;/strong&gt; objects.&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%2f07%2f09%2ftesting-and-debugging-mef-tips-part-1.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-Tips-Part-1-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%2F07%2F09%2Ftesting-and-debugging-mef-tips-part-1.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%2f07%2f09%2ftesting-and-debugging-mef-tips-part-1.aspx&amp;amp;title=Testing+and+Debugging+MEF%2c+Tips+-+Part+1"&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=677606" 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/.net/default.aspx">.net</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/Testing/default.aspx">Testing</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Mock/default.aspx">Mock</category></item><item><title>MEF for Beginner (Deployment Catalog) - part 12</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/04/01/mef-for-beginner-deployment-catalog-part-12.aspx</link><pubDate>Fri, 02 Apr 2010 01:20:55 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:572185</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;MEF for Beginner (Deployment Catalog) - part 12&lt;/h2&gt;  &lt;p&gt;&lt;img style="border-right-width:0px;margin:0px 15px 0px 0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="deployment catalog,MEF,Import,Importmany,export,compose,composition" border="0" alt="deployment catalog,MEF,Import,Importmany,export,compose,composition" align="left" src="http://blogs.microsoft.co.il/blogs/bnaya/deploymentcatalog_2D5B1AB5.jpg" width="131" height="156" /&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;this is the 12th post of the &lt;a href="http://mef.codeplex.com/"&gt;&lt;strong&gt;MEF&lt;/strong&gt;&lt;/a&gt; for Beginner series, the series &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/01/09/mef-for-beginner-toc.aspx"&gt;TOC&lt;/a&gt; is available &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/01/09/mef-for-beginner-toc.aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;this post will focus on&lt;strong&gt; Deployment Catalog&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;the code sample for this post can be found &lt;a href="http://cid-9bf7c1a515d76a9a.skydrive.live.com/browse.aspx/Code%20Samples/MEF/MEF%20for%20Beginners/Part%2012?uc=1&amp;amp;isFromRichUpload=1" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;&amp;#160;&lt;/h5&gt;  &lt;h5&gt;What is MEF Deployment Catalogs?&lt;/h5&gt;  &lt;p&gt;the &lt;strong&gt;deployment catalog&lt;/strong&gt; is actually a redesign of the older&lt;strong&gt; package catalog&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;it enable to &lt;strong&gt;load parts&lt;/strong&gt; from &lt;strong&gt;xap&lt;/strong&gt; packages a-synchronically.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Code sample&lt;/h5&gt;  &lt;p&gt;the following code sample depend on 2 assembly that should be added to the project:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;System.ComponentModel.Composition &lt;/li&gt;    &lt;li&gt;System.ComponentModel.Composition.Initialization &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;at the startup (&lt;strong&gt;app.xaml&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:1445eb1a-5ba2-480c-82bd-bd8ae0324ac5" 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;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Application_Startup(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; sender,&lt;span style="color:#2b91af;"&gt;StartupEventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;InitializeContainer();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;.RootVisual = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MainPage&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;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; InitializeContainer()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; catalogs = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;AggregateCatalog&lt;/span&gt;();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&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;TypeCatalog&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;PlugIns&lt;/span&gt;));&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;catalogs.Catalogs.Add(catalog);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#008000;"&gt;// Create deployment catalog&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; deployCatalog = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;DeploymentCatalog&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;PlugIns.xap&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;deployCatalog.DownloadCompleted += OnDownloadCompletedHandler;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;deployCatalog.DownloadProgressChanged += OnDownloadProgressChangedHandler;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;deployCatalog.DownloadAsync();&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;catalogs.Catalogs.Add(deployCatalog);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#008000;"&gt;// initialize the main application composition host (container)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#2b91af;"&gt;CompositionHost&lt;/span&gt;.Initialize(catalogs);&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;strong&gt;line 3&lt;/strong&gt;, &lt;strong&gt;initialize&lt;/strong&gt; &lt;strong&gt;MEF&lt;/strong&gt; before the creation of any windows that depend on MEF.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line 9&lt;/strong&gt;, define &lt;strong&gt;aggregate catalog&lt;/strong&gt; (this is basically a catalog collection). &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;lines 11-12&lt;/strong&gt;, create &lt;strong&gt;type catalog&lt;/strong&gt; for plug-ins within the &lt;strong&gt;main&lt;/strong&gt; &lt;strong&gt;xap&lt;/strong&gt; (plug-in within the main xap&lt;/p&gt;  &lt;p&gt;can be assess directly) and add the catalog to the &lt;strong&gt;aggregate catalog&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line 15&lt;/strong&gt;, create &lt;strong&gt;deployment catalog&lt;/strong&gt; (it get the xap relative path as parameter).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;lines 16-17&lt;/strong&gt;, register for &lt;strong&gt;xap&lt;/strong&gt; &lt;strong&gt;download process&lt;/strong&gt; events (&lt;strong&gt;optional&lt;/strong&gt;).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line 18&lt;/strong&gt;, start &lt;strong&gt;downloading&lt;/strong&gt; the xap.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line 20&lt;/strong&gt;, add the &lt;strong&gt;deployment catalog&lt;/strong&gt; to the &lt;strong&gt;aggregate catalog&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line 23&lt;/strong&gt;, &lt;strong&gt;initialize&lt;/strong&gt; the &lt;strong&gt;composition host&lt;/strong&gt; with the &lt;strong&gt;aggregate catalog&lt;/strong&gt; (so &lt;/p&gt;  &lt;p&gt;latter using &lt;strong&gt;CompositionInitializer.SatisfyImports&lt;/strong&gt; will operate against the &lt;strong&gt;aggregate catalog&lt;/strong&gt;).&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;at the &lt;strong&gt;MainPage.xaml&lt;/strong&gt; the code will look as follow:&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:ac24aa17-da47-4416-b1c8-eca20f21e845" 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;partial&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MainPage&lt;/span&gt;:&lt;span style="color:#2b91af;"&gt;UserControl&lt;/span&gt;,&lt;span style="color:#2b91af;"&gt;INotifyPropertyChanged&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; MainPage()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;InitializeComponent();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;.DataContext = &lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&amp;gt; _plugIns;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;[&lt;span style="color:#2b91af;"&gt;ImportMany&lt;/span&gt;(AllowRecomposition = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&amp;gt; PlugIns&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _plugIns;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;_plugIns = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;(PropertyChanged != &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;PropertyChanged(&lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;,&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;PropertyChangedEventArgs&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;PlugIns&amp;quot;&lt;/span&gt;));&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;event&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;PropertyChangedEventHandler&lt;/span&gt; PropertyChanged;&lt;/li&gt; &lt;li&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;strong&gt;line 7&lt;/strong&gt;, asking &lt;strong&gt;MEF&lt;/strong&gt; to &lt;strong&gt;satisfy&lt;/strong&gt; any &lt;strong&gt;import&lt;/strong&gt; of the current instance (in our case the PlugIns property).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;lines 11&lt;/strong&gt;, decorate the &lt;strong&gt;PlugIns&lt;/strong&gt; property for being assigned by the &lt;strong&gt;MEF&lt;/strong&gt; composition.&lt;/p&gt;  &lt;p&gt;notice that the decoration define &lt;strong&gt;AllowRecomposition = true, &lt;/strong&gt;this is very important&lt;/p&gt;  &lt;p&gt;because the &lt;strong&gt;deployment catalog&lt;/strong&gt; is &lt;strong&gt;a-synchronic,&lt;/strong&gt; therefore we do not control the exact time of the assignment.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;&lt;strong&gt;deployment catalog&lt;/strong&gt; is the current way for &lt;strong&gt;delay composition&lt;/strong&gt; from &lt;strong&gt;xap&lt;/strong&gt; packages.&lt;/p&gt;  &lt;p&gt;it is a-synchronic and it will notify the catalog upon the download completion.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;the code sample for this post can be found &lt;a href="http://cid-9bf7c1a515d76a9a.skydrive.live.com/browse.aspx/Code%20Samples/MEF/MEF%20for%20Beginners/Part%2012?uc=1&amp;amp;isFromRichUpload=1" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Learn more&lt;/h5&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a title="http://codebetter.com/blogs/glenn.block/archive/2010/03/07/building-hello-mef-part-iv-deploymentcatalog.aspx" href="http://codebetter.com/blogs/glenn.block/archive/2010/03/07/building-hello-mef-part-iv-deploymentcatalog.aspx"&gt;http://codebetter.com/blogs/glenn.block/archive/2010/03/07/building-hello-mef-part-iv-deploymentcatalog.aspx&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a title="http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2010/02/24/mef-silverlight-and-the-deploymentcatalog.aspx" href="http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2010/02/24/mef-silverlight-and-the-deploymentcatalog.aspx"&gt;http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2010/02/24/mef-silverlight-and-the-deploymentcatalog.aspx&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%2f04%2f01%2fmef-for-beginner-deployment-catalog-part-12.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2010%2f04%2f01%2fmef-for-beginner-deployment-catalog-part-12.aspx" /&gt;&lt;/a&gt; &lt;a href="http://dotnetshoutout.com/MEF-for-Beginner-Deployment-Catalog-part-12-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%2F04%2F01%2Fmef-for-beginner-deployment-catalog-part-12.aspx" /&gt;&lt;/a&gt;&lt;div class="wlWriterHeaderFooter" style="margin:0px;padding:0px 0px 0px 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"&gt;&lt;/script&gt;&lt;/div&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=572185" 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/Beginner/default.aspx">Beginner</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/CLR+4/default.aspx">CLR 4</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/.net/default.aspx">.net</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/IoC/default.aspx">IoC</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></item><item><title>MEF for Beginner (Import from Xaml) - part 11</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/03/20/mef-for-beginner-import-from-xaml-part-11.aspx</link><pubDate>Sat, 20 Mar 2010 21:28:23 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:552959</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;MEF for Beginner (Import from Xaml) - part 11&lt;/h2&gt;  &lt;p&gt;this is the 11th post of the &lt;a href="http://mef.codeplex.com/"&gt;&lt;strong&gt;MEF&lt;/strong&gt;&lt;/a&gt; for Beginner series, the series &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/01/09/mef-for-beginner-toc.aspx"&gt;TOC&lt;/a&gt; is available &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/01/09/mef-for-beginner-toc.aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;this post will focus on &lt;strong&gt;Importing mef parts &lt;/strong&gt;directly from the &lt;strong&gt;Xaml&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/iStock_000005438250VerySmallXmXorl_0339C5C9.jpg"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="Import, ImportMany, MEF, composition, CLR 4" border="0" alt="Import, ImportMany, MEF, composition, CLR 4" src="http://blogs.microsoft.co.il/blogs/bnaya/iStock_000005438250VerySmallXmXorl_thumb_2E6E3703.jpg" width="303" height="77" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;the code sample for this post is available &lt;a href="http://cid-9bf7c1a515d76a9a.skydrive.live.com/browse.aspx/Code%20Samples/MEF/MEF%20for%20Beginners/Part%2011?uc=1&amp;amp;isFromRichUpload=1" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;assuming that we have the following &lt;strong&gt;exports&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:3c19987e-7cb2-4576-8452-1bb91342eeae" 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;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;DemoStrings&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;MyTag&amp;quot;&lt;/span&gt;)]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Text1 { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#a31515;"&gt;&amp;quot;Hello world&amp;quot;&lt;/span&gt;; } }&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;MyTag&amp;quot;&lt;/span&gt;)]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Text2 { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#a31515;"&gt;&amp;quot;export using custom attribute&amp;quot;&lt;/span&gt;; } }&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;NotMyTag&amp;quot;&lt;/span&gt;)]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Text3 { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#a31515;"&gt;&amp;quot;not included (Not My Tag contract)&amp;quot;&lt;/span&gt;; } }&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;MyTag&amp;quot;&lt;/span&gt;)]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Text4 { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#a31515;"&gt;&amp;quot;Wpf markup extension&amp;quot;&lt;/span&gt;; } }&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Text5 { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#a31515;"&gt;&amp;quot;not included (have no contract)&amp;quot;&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;&amp;#160;&lt;/p&gt;  &lt;p&gt;we may want to have it &lt;strong&gt;import&lt;/strong&gt; into the &lt;strong&gt;Xaml&lt;/strong&gt; as follow:&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:8f4b3bb2-cced-4c46-9881-b1d7f42810a4" 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;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Window&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; x&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;Class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Bnaya.Samples.MainWindow&amp;quot;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#ff0000;"&gt; xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#ff0000;"&gt; xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;x&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#ff0000;"&gt; xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;mef&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;clr-namespace:Bnaya.Samples&amp;quot;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#ff0000;"&gt; Title&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;MainWindow&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; Height&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;350&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; Width&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;525&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#a31515;"&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Grid&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#a31515;"&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;ListBox&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#ff0000;"&gt; DataContext&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color:#a31515;"&gt;mef&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;:&lt;/span&gt;&lt;span style="color:#a31515;"&gt;ImportManyStrings&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; MyTag}&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;quot;&lt;/span&gt; &lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#ff0000;"&gt; ItemsSource&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Binding}&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;quot;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#a31515;"&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Grid&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Window&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&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 at &lt;strong&gt;line 8&lt;/strong&gt; the &lt;strong&gt;data context&lt;/strong&gt; is using special definition for &lt;strong&gt;importing&lt;/strong&gt; all &lt;strong&gt;exported&lt;/strong&gt; &lt;strong&gt;strings&lt;/strong&gt; that     &lt;br /&gt;define the their &lt;strong&gt;contract&lt;/strong&gt; as &lt;strong&gt;MyTag&lt;/strong&gt; (&lt;font color="#808080"&gt;looking at the &lt;strong&gt;exports&lt;/strong&gt; it will &lt;strong&gt;import&lt;/strong&gt; the exports at&lt;strong&gt; lines 4,6,10&lt;/strong&gt;&lt;/font&gt;).&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;what should we have in order to achieve that functionality?&lt;/h5&gt;  &lt;p&gt;actually very little, all we have to do is to define a&lt;strong&gt; markup extension&lt;/strong&gt; that return the &lt;strong&gt;exported&lt;/strong&gt; &lt;strong&gt;parts&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;the following code is how the &lt;strong&gt;markup extension&lt;/strong&gt; should be define:&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:ce4d674f-f189-46ff-82bc-0b4ea7447c79" 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;MarkupExtensionReturnType&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&amp;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;ImportManyStringsExtension&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;MarkupExtension&lt;/span&gt;&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&amp;gt; _dataSource;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ImportManyStringsExtension(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; contract)&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;_dataSource = &lt;span style="color:#2b91af;"&gt;CompositionHost&lt;/span&gt;.Provider.GetExportedValues&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&amp;gt;(contract);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; ProvideValue(&lt;span style="color:#2b91af;"&gt;IServiceProvider&lt;/span&gt; serviceProvider)&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _dataSource;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&amp;#160;&amp;#160;&amp;#160;&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;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line 1&lt;/strong&gt;, decorate that our &lt;strong&gt;markup extension&lt;/strong&gt; for returning &lt;strong&gt;IEnumerable&amp;lt;string&amp;gt;&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line 2&lt;/strong&gt;, derive from &lt;strong&gt;MarkupExtension&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line 7&lt;/strong&gt;, at the construction time, dynamically ask &lt;strong&gt;mef&lt;/strong&gt; for any &lt;strong&gt;discoverable parts&lt;/strong&gt; that follow the &lt;strong&gt;contract&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;font color="#808080"&gt;the CompositionHost is a very small helper class that was taken from the &lt;strong&gt;MEF Silverlight&lt;/strong&gt; implementation &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#808080"&gt;with some very minor enhancements&lt;/font&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;lines 10-13&lt;/strong&gt;, returning the &lt;strong&gt;discoverable parts&lt;/strong&gt; to the &lt;strong&gt;Xaml&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;One last thing&lt;/h5&gt;  &lt;p&gt;we should remove the &lt;strong&gt;StartupUri&lt;/strong&gt; from the &lt;strong&gt;App.Xaml&lt;/strong&gt; and using the &lt;strong&gt;OnStartup override&lt;/strong&gt; instead:&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:a18e7edc-77f1-43b8-8db2-338a4a86aea3" 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;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; OnStartup(&lt;span style="color:#2b91af;"&gt;StartupEventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;base&lt;/span&gt;.OnStartup(e);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (!&lt;span style="color:#2b91af;"&gt;Utils&lt;/span&gt;.IsInDesignTool)&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; asmCatalog = &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;App&lt;/span&gt;).Assembly);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; catalogs = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;AggregateCatalog&lt;/span&gt;(asmCatalog);&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&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;(catalogs);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#2b91af;"&gt;CompositionHost&lt;/span&gt;.Initialize(container);&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;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 style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; root = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MainWindow&lt;/span&gt;(); &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;root.ShowDialog();&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&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;strong&gt;line 5&lt;/strong&gt;, preventing &lt;strong&gt;MEF&lt;/strong&gt; operation during&lt;strong&gt; design time&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;lines 7-9&lt;/strong&gt;, setting the&lt;strong&gt; MEF container&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line 10&lt;/strong&gt;, setting the &lt;strong&gt;MEF container&lt;/strong&gt; as the &lt;strong&gt;main container&lt;/strong&gt; of the application &lt;/p&gt;  &lt;p&gt;(this way it can be &lt;strong&gt;reachable anywhere&lt;/strong&gt;).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line 11&lt;/strong&gt;, do the &lt;strong&gt;composition&lt;/strong&gt;, &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color="#ff0000"&gt;IMPOERANT&lt;/font&gt;&lt;/strong&gt;: the &lt;strong&gt;composition&lt;/strong&gt; should happens &lt;strong&gt;before&lt;/strong&gt; the &lt;strong&gt;window instantiation&lt;/strong&gt;, &lt;/p&gt;  &lt;p&gt;because that when the &lt;strong&gt;markup extension&lt;/strong&gt; will be invoke.&lt;/p&gt;  &lt;p&gt;the &lt;strong&gt;exported part&lt;/strong&gt; should be available at this time. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;lines 13-14&lt;/strong&gt;, starting the application.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;we can extend the &lt;strong&gt;Xaml&lt;/strong&gt; very easily in order to get better &lt;strong&gt;Xaml&lt;/strong&gt;-&amp;gt;&lt;strong&gt;Mef&lt;/strong&gt; experience.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;the code sample for this post is available &lt;a href="http://cid-9bf7c1a515d76a9a.skydrive.live.com/browse.aspx/Code%20Samples/MEF/MEF%20for%20Beginners/Part%2011?uc=1&amp;amp;isFromRichUpload=1" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&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:0767317B-992E-4b12-91E0-4F059A8CECA8:1bd718e5-fd3d-4d99-80e3-d80d7f7d4f74" class="wlWriterSmartContent"&gt;תגים של Technorati:‏ &lt;a href="http://technorati.com/tags/Import" rel="tag"&gt;Import&lt;/a&gt;,&lt;a href="http://technorati.com/tags/ImportMany" rel="tag"&gt;ImportMany&lt;/a&gt;,&lt;a href="http://technorati.com/tags/MEF" rel="tag"&gt;MEF&lt;/a&gt;,&lt;a href="http://technorati.com/tags/composition" rel="tag"&gt;composition&lt;/a&gt;,&lt;a href="http://technorati.com/tags/CLR+4" rel="tag"&gt;CLR 4&lt;/a&gt;&lt;/div&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%2f03%2f20%2fmef-for-beginner-import-from-xaml-part-11.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2010%2f03%2f20%2fmef-for-beginner-import-from-xaml-part-11.aspx" /&gt;&lt;/a&gt;&lt;div class="wlWriterHeaderFooter" style="margin:0px;padding:0px 0px 0px 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"&gt;&lt;/script&gt;&lt;/div&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=552959" 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/WPF/default.aspx">WPF</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/Beginner/default.aspx">Beginner</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/.net/default.aspx">.net</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></item></channel></rss>