<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/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>My Technobabble : MEF</title><link>http://blogs.msdn.com/b/gblock/archive/tags/MEF/</link><description>Tags: MEF</description><dc:language>en</dc:language><generator>Telligent Community 5.6.583.21163 (Build: 5.6.583.21163)</generator><item><title>Bringing MEF and REST love to the UK and at FEST 10</title><link>http://blogs.msdn.com/b/gblock/archive/2010/07/11/bringing-mef-and-rest-love-to-the-uk-and-at-fest-10.aspx</link><pubDate>Sun, 11 Jul 2010 02:18:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10036825</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=10036825</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2010/07/11/bringing-mef-and-rest-love-to-the-uk-and-at-fest-10.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-73-92-metablogapi/2577.image_5F00_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-73-92-metablogapi/5148.image_5F00_thumb.png" width="691" height="373" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Next week I’m coming to the UK where I’ll be spreading the MEF love at FEST 10 and in events in a few other cities. I’ll also be talking about REST and the new work we are doing in WCF to provide a better experience for building RESTful systems. &lt;/p&gt;  &lt;p&gt;Below is the event schedule&lt;/p&gt;  &lt;table border="0" cellspacing="0" cellpadding="2" width="925"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="132"&gt;Date/Time&lt;/td&gt;        &lt;td valign="top" width="96"&gt;Event&lt;/td&gt;        &lt;td valign="top" width="112"&gt;Location&lt;/td&gt;        &lt;td valign="top" width="583"&gt;Talk(s)&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="132"&gt;14 July 2010          &lt;br /&gt;10:00 AM to 2:00 PM&lt;/td&gt;        &lt;td valign="top" width="96"&gt;&lt;a href="http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2010/06/03/microsoft-uk-event-managed-extensibility-framework-mef-with-glenn-block.aspx"&gt;Microsoft UK&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="112"&gt;Microsoft Campus          &lt;br /&gt;Reading&lt;/td&gt;        &lt;td valign="top" width="583"&gt;&lt;strong&gt;.NET Managed Extensibility Framework with Glenn Block [A half day of MEF]&lt;/strong&gt;           &lt;br /&gt;          &lt;br /&gt;          &lt;p&gt;The Managed Extensibility Framework (MEF) is a new library which is part of the .Net 4.0 framework. MEF helps you build extensible and maintainable applications, which you can evolve over time, by providing a mechanism that promotes decoupling and separation of concerns, and supports plugins.&lt;/p&gt;          &lt;p&gt;Come join us on this quest for enlightenment in the way of MEF. We will start our journey light with the essentials of composition in MEF: Import, Export and Compose. We will then travel together on the path as we elevate toward master.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="132"&gt;14 July 2010&amp;#160; &lt;br /&gt;7:00 PM to 9:00 PM&lt;/td&gt;        &lt;td valign="top" width="96"&gt;&lt;a href="http://www.nxtgenug.net/ViewEvent.aspx?EventID=327"&gt;NxtGenUG&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="112"&gt;Microsoft Research Cambridge&lt;/td&gt;        &lt;td valign="top" width="583"&gt;&lt;strong&gt;Silverlight Application Partitioning and Extensibility with MEF&lt;/strong&gt;           &lt;br /&gt;          &lt;br /&gt;Wouldn't it be nice if your team could add new features to your applications without all the headache they have to deal with today? With the Managed Extensiblity Framework (MEF) in 4.0 you can bolt your apps together dynamically on the fly. Adding new modules and features is as simple as deploying a new binary, you don't have to touch the existing code. You can even deploy those features as separate XAP files which can be loaded on-demand! Come to this talk for a quick tour of what MEF is, and learn how you can use it to beat out your competition.&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="132"&gt;15 July 2010 Evening&lt;/td&gt;        &lt;td valign="top" width="96"&gt;Geek Dinner&lt;/td&gt;        &lt;td valign="top" width="112"&gt;Somewhere in London near Victoria&lt;/td&gt;        &lt;td valign="top" width="583"&gt;Register here quick: &lt;a title="http://is.gd/dolgi" href="http://is.gd/dolgi"&gt;http://is.gd/dolgi&lt;/a&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="132"&gt;16 July 2010 &lt;/td&gt;        &lt;td valign="top" width="96"&gt;&lt;a href="http://www.nxtgenug.net/Fest10/Sessions.aspx"&gt;Fest 2010!&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="112"&gt;Bournemouth Pier          &lt;br /&gt;Bournemouth&lt;/td&gt;        &lt;td valign="top" width="583"&gt;&lt;strong&gt;Building extensible frameworks with MEF&lt;/strong&gt;           &lt;br /&gt;          &lt;br /&gt;Come to this talk where we will take a deep dive into building frameworks which can easily extended by third-parties. For the first part of the session we will learn about what MEF offers for framework authors as we build a small extensible library which leverages it. Then in the second part we’ll dive into several OSS frameworks that are utilizing MEF for extensibility. We’ll also leave plenty of time to answer any deep questions you have on MEF.           &lt;br /&gt;          &lt;br /&gt;&lt;strong&gt;REST, It’s more than Data&lt;/strong&gt;           &lt;br /&gt;          &lt;br /&gt;Today it’s very common to simply throw some data over the wire and call it REST. But REST as Roy Fielding described it is much, much more. Come to this session and we’ll explore the tenants of a RESTful system. We’ll start with looking at traditional web services, then we’ll remove the SOAP shackles and explore what we can achieve as we get closer and closer to HTTP. We’ll also look at various frameworks both within and outside Microsoft for achieving REST Nirvana.&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;&lt;strong&gt;All of this is made possible by the fine folks at &lt;/strong&gt;&lt;a href="http://www.nxtgenug.net/"&gt;&lt;strong&gt;NxtGenUG&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt; who have been gracious to sponsor my trip. &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I look forward to seeing all of you on the other side of the Atlantic!!! &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10036825" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/gblock/archive/tags/silverlight/">silverlight</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/REST/">REST</category></item><item><title>Open sourced .NET libraries in Mono</title><link>http://blogs.msdn.com/b/gblock/archive/2010/07/08/open-sourced-net-libraries-in-mono.aspx</link><pubDate>Thu, 08 Jul 2010 08:46:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10035817</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=10035817</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2010/07/08/open-sourced-net-libraries-in-mono.aspx#comments</comments><description>&lt;p&gt;Recently &lt;a href="http://tirania.org/blog/archive/2010/Jul-07.html"&gt;Miguel&lt;/a&gt; did a &lt;a href="http://tirania.org/blog/archive/2010/Jul-07.html"&gt;post&lt;/a&gt; that made me smile. He talked about several libraries from Microsoft which were released under an OSS license and thus incorporated into the Mono tree and ship as part of Mono 2.8. I smiled for two reasons. One, I was happy to see that Microsoft is becoming more and more open with our development platform. Second, I was happy because MEF was on that list. :-)&lt;/p&gt;  &lt;p&gt;The full list is below:&lt;/p&gt;  &lt;ul&gt;&lt;li&gt;&lt;a href="http://mef.codeplex.com/"&gt;Managed Extensibility Framework&lt;/a&gt;, a framework to create extensible applications (&lt;a href="http://tirania.org/blog/archive/2008/Oct-02.html"&gt;blogged about their open sourcing&lt;/a&gt;). &lt;/li&gt;  &lt;li&gt;The &lt;a href="http://dlr.codeplex.com/"&gt;Dynamic Language Runtime&lt;/a&gt; is the engine that powers IronPython and IronRuby, but also third party efforts like IronJS and RemObject's Javascript compiler. This lives largely in the System.Core assembly in .NET 4. Some of the code is also used in Mono's version of System.Numerics. &lt;/li&gt;  &lt;li&gt;OData's .NET client (System.Data.Services.Client), I blogged about this &lt;a href="http://tirania.org/blog/archive/2010/Mar-22.html"&gt;here&lt;/a&gt;. &lt;/li&gt;  &lt;li&gt;&lt;a href="http://www.asp.net/mvc"&gt;ASP.NET MVC&lt;/a&gt; both MVC1 and MVC2. &lt;/li&gt;  &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/bb398874.aspx"&gt;Microsoft's AJAX&lt;/a&gt; library for use with ASP.NET WebForms. &lt;/li&gt;  &lt;li&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=EB83ED4C-AC85-4DE9-8395-285628EE2254&amp;amp;displaylang=en"&gt;Silverlight's Controls&lt;/a&gt;, these are used by &lt;a href="http://www.go-mono.com/moonlight"&gt;Moonlight&lt;/a&gt;.&lt;/li&gt;  &lt;/ul&gt;&lt;p&gt;I hope this is just the beginning!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10035817" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MSPL/">MSPL</category></item><item><title>Way of MEF slides and Code</title><link>http://blogs.msdn.com/b/gblock/archive/2010/06/13/way-of-mef-slides-and-code.aspx</link><pubDate>Sun, 13 Jun 2010 07:11:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10024050</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=10024050</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2010/06/13/way-of-mef-slides-and-code.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-73-92-metablogapi/3782.image_5F00_4.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-73-92-metablogapi/3617.image_5F00_thumb_5F00_1.png" width="599" height="450" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Thanks to everyone who came out to the &lt;a href="http://codebetter.com/blogs/glenn.block/archive/2010/06/03/mef-day-in-sf-the-way-of-mef-the-path-to-enlightenment.aspx"&gt;“Way of MEF”&lt;/a&gt; education day in Cupertino. I had a great time and you were an awesome audience. I really appreciate all the great / in-depth questions. It showed that you really were keeping along. I also enjoyed all the different side conversations, and all the times you guys overwhelmed me at the breaks. I really do love this stuff!&lt;/p&gt;  &lt;p&gt;Here is a link to get you access to the code and the slides: &lt;a title="http://cid-f8b2fd72406fb218.office.live.com/browse.aspx/Public/Way%20of%20MEF" href="http://cid-f8b2fd72406fb218.office.live.com/browse.aspx/Public/Way%20of%20MEF"&gt;http://cid-f8b2fd72406fb218.office.live.com/browse.aspx/Public/Way%20of%20MEF&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;If you download the code it contains the following:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;HelloMef_Simple - The simple widget example we built &lt;/li&gt;    &lt;li&gt;HelloMef_Extended - Sample that downloads multiple XAPs and which demonstrates ExportFactory. &lt;/li&gt;    &lt;li&gt;MefRules_Simple - The simple rules engine we built on the fly &lt;/li&gt;    &lt;li&gt;MefRules_Extended - More complete rules engine sample which uses rule metadata and contains a runner for Silverlight and Desktop. &lt;/li&gt;    &lt;li&gt;DynamicObjectContracts - Demonstrates a simple illustration of using dynamic exports and imports &lt;/li&gt;    &lt;li&gt;PartUpdatesInPlace - Demonstrates uses shadow copying to allow hot swapping / removing parts on the fly. (the one that the audience asked me about) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Here are additional links:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Default exports: &lt;a href="http://blogs.msdn.com/b/gblock/archive/2009/05/14/customizing-container-behavior-part-2-of-n-defaults.aspx"&gt;http://blogs.msdn.com/b/gblock/archive/2009/05/14/customizing-container-behavior-part-2-of-n-defaults.aspx&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Filtered Catalogs: &lt;a href="http://mef.codeplex.com/wikipage?title=Filtering%20Catalogs&amp;amp;referringTitle=Guide"&gt;http://mef.codeplex.com/wikipage?title=Filtering%20Catalogs&amp;amp;referringTitle=Guide&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Config export provider: &lt;a href="http://codepaste.net/en44vo"&gt;http://codepaste.net/en44vo&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Ruby and MEF: &lt;a title="http://blogs.msdn.com/b/nblumhardt/archive/tags/ruby/" href="http://blogs.msdn.com/b/nblumhardt/archive/tags/ruby/"&gt;http://blogs.msdn.com/b/nblumhardt/archive/tags/ruby/&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;ASP.NET MVC and MEF: &lt;a title="MEF and ASP.NET MVC sample updated" href="http://blogs.msdn.com/b/hammett/archive/2009/07/15/mef-and-asp-net-mvc-sample-updated.aspx"&gt;MEF and ASP.NET MVC sample updated&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;ASP.NET Web Forms and MEF (doesn’t use CompositionInitializer approach): &lt;a title="WebForms and MEF Sample" href="http://mef.codeplex.com/releases/view/44166"&gt;WebForms and MEF Sample&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Diagnosing MEF - &lt;a title="http://blogs.msdn.com/b/nblumhardt/archive/2009/08/28/analyze-mef-assemblies-from-the-command-line.aspx" href="http://blogs.msdn.com/b/nblumhardt/archive/2009/08/28/analyze-mef-assemblies-from-the-command-line.aspx"&gt;http://blogs.msdn.com/b/nblumhardt/archive/2009/08/28/analyze-mef-assemblies-from-the-command-line.aspx&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Finally as promised, if you have questions that were not answered in the talk, feel free to comment here or email me direct and I will answer them in a follow up post.&lt;/p&gt;  &lt;p&gt;Thanks to &lt;a href="http://blog.brandewinder.com/"&gt;Mathias Brandewinder&lt;/a&gt;, &lt;a href="http://blog.brandewinder.com/"&gt;Kevin Rohling&lt;/a&gt;, his &lt;a href="http://orlandotechnuts.blogspot.com/"&gt;nutty&lt;/a&gt; girlfriend and recruiter extraordinaire Megan Hopkins and the Bay.NET UG for hosting me. I had an awesome time.&lt;/p&gt;  &lt;p&gt;Yes, I know I need to post on what I am working on now……I hear you, pleae be patient :-)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10024050" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category></item><item><title>MEF day in SF, “The Way of MEF, the Path to Enlightenment”</title><link>http://blogs.msdn.com/b/gblock/archive/2010/06/03/mef-day-in-sf-the-way-of-mef-the-path-to-enlightenment.aspx</link><pubDate>Thu, 03 Jun 2010 08:08:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10019355</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=10019355</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2010/06/03/mef-day-in-sf-the-way-of-mef-the-path-to-enlightenment.aspx#comments</comments><description>&lt;p&gt;&lt;img src="http://www.clear-lines.com/wiki/public/upload/misc/MEF-Fu.png" /&gt;&lt;/p&gt;  &lt;p&gt;On Saturday June 12th I am stoked to be coming to the SF bay area on behalf of the Bay.Net UG where I’ll be delivering a full day on MEF! &lt;/p&gt;  &lt;p&gt;We’re going to cover a world of topics as you can see from the list below.&lt;/p&gt;  &lt;p&gt;1. How to lazily instantiate parts to conserve processor cycles   &lt;br /&gt;2. Self-describing parts in order to allow more flexible discovery    &lt;br /&gt;3. Diagnosing common MEF failures    &lt;br /&gt;4. Adapting legacy components to work with MEF    &lt;br /&gt;5. Partitioning your Silverlight applications across multiple XAPs to reduce download cost and improve startup time    &lt;br /&gt;6. Providing default overridable parts    &lt;br /&gt;7. Using hierarchical containers for scoped composition    &lt;br /&gt;8. Creating custom catalogs to apply custom policy for filtering    &lt;br /&gt;9. Creating export providers to integrate with existing service providers    &lt;br /&gt;10. Composing UIs with MEF / MEF with Prism    &lt;br /&gt;11. Implementing MVVM with MEF&lt;/p&gt;  &lt;p&gt;The cost is $39.&amp;#160; Register &lt;a href="http://acteva.com/booking.cfm?bevaID=204633"&gt;here&lt;/a&gt; while there is still time!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10019355" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category></item><item><title>MEF has shipped!!!</title><link>http://blogs.msdn.com/b/gblock/archive/2010/06/03/mef-has-shipped.aspx</link><pubDate>Thu, 03 Jun 2010 01:26:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10019238</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=10019238</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2010/06/03/mef-has-shipped.aspx#comments</comments><description>&lt;p&gt;You might have missed it, but a few weeks ago &lt;strong&gt;MEF V1 shipped in &lt;/strong&gt;&lt;a href="http://www.microsoft.com/net/"&gt;&lt;strong&gt;.NET 4.0&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt; and &lt;/strong&gt;&lt;a href="http://www.microsoft.com/silverlight/"&gt;&lt;strong&gt;Silverlight 4&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;! &lt;/strong&gt;As a team we&amp;rsquo;re really happy to have seen &lt;a href="http://msdn.microsoft.com/en-us/magazine/ee291628.aspx"&gt;MEF&lt;/a&gt; hit the wild. We believe MEF helps you to build better applications that are easier to extend. To get started with MEF, go visit our wiki &lt;a href="http://mef.codeplex.com/documentation"&gt;here&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;So far we&amp;rsquo;ve been hearing lots of goodness from customers that they feel the same. We&amp;rsquo;ve also been really happy seeing a healthy set of projects in the open source community pulling in MEF to address their extensibility challenges within their frameworks and applications. We&amp;rsquo;ve also had some interesting ports of MEF which is a great outcome of having an &lt;a href="http://codebetter.com/blogs/glenn.block/archive/2008/10/02/mef-going-ms-pl-the-little-engine-that-could.aspx"&gt;OSS license&lt;/a&gt;!&lt;/p&gt;
&lt;p&gt;Below is a list of some of the folks internal and external to Microsoft that are using MEF along with the scenarios for which they use/support us (in a completely random order)&lt;/p&gt;
&lt;table cellpadding="2" cellspacing="0" border="1" style="width: 788px;"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td width="241" valign="top"&gt;&lt;a href="http://0.r.msn.com/?ld=2vWdp/GaoTcWGCboTNuq0bo4XjcXJ7/RfWEvJXU8ajAllNIiuXkyVDT8eP1FNjAmlEvsQ6wqAHfYQmFpWDNYmwvR1zvirHTWw9kiUCslzvyjguHWxVtcWB39hFG1BqB+GwfhCb1keiTmWMX2MSbeqF9M0wMNk2WrxB9/tXsx7rwXV/nyogowvImoWPy+W2WTBTbKe0XRadeyJMqPkUD3I4+8Az68zYkq7jnp0Nkk1rkmNgzdQ2kAK0oQx413XHXOQUJExd16ToAkakuCI9D6ZMyzkGoyaB/1q28EhPR7Lz98Qbxv8xdXk4Cw=="&gt;Visual Studio 2010&lt;/a&gt;&lt;/td&gt;
&lt;td width="545" valign="top"&gt;
&lt;ul&gt;
&lt;li&gt;New extensible editor &lt;/li&gt;
&lt;li&gt;Project system uses MEF under the covers &lt;/li&gt;
&lt;li&gt;VS Ultimate uses MEF for it&amp;rsquo;s UML designers &lt;/li&gt;
&lt;li&gt;Several other teams like EF use MEF for their designers &lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="241" valign="top"&gt;&lt;a href="http://mefcontrib.codeplex.com"&gt;MEF Contrib&lt;/a&gt;&lt;/td&gt;
&lt;td width="545" valign="top"&gt;
&lt;ul&gt;
&lt;li&gt;Community driven extensions to MEF &lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="258" valign="top"&gt;&lt;a href="http://msaf.codeplex.com/"&gt;Silverlight Analytics Framework&lt;/a&gt;&lt;/td&gt;
&lt;td width="565" valign="top"&gt;
&lt;ul&gt;
&lt;li&gt;Allows plugging in different analytic engines through MEF &lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="264" valign="top"&gt;&lt;a href="http://smf.codeplex.com/"&gt;Silverlight Media Framework&lt;/a&gt;&lt;/td&gt;
&lt;td width="567" valign="top"&gt;
&lt;ul&gt;
&lt;li&gt;Uses MEF for plugging in various extensions relating to the player &lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="268" valign="top"&gt;&lt;a href="http://site.typemock.com/test-lint"&gt;TypeMock Test Lint&lt;/a&gt;&lt;/td&gt;
&lt;td width="565" valign="top"&gt;
&lt;ul&gt;
&lt;li&gt;Uses MEF for allowing you to plug in your own custom rules &lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="270" valign="top"&gt;&lt;a href="http://ravendb.net/"&gt;RavenDB&lt;/a&gt;&lt;/td&gt;
&lt;td width="563" valign="top"&gt;
&lt;ul&gt;
&lt;li&gt;Uses MEF to allow plugging in triggers &lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="272" valign="top"&gt;&lt;a href="http://caliburn.codeplex.com/"&gt;Caliburn&lt;/a&gt;&lt;/td&gt;
&lt;td width="562" valign="top"&gt;
&lt;ul&gt;
&lt;li&gt;Supports plugging in MEF as a service provider &lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="273" valign="top"&gt;&lt;a href="http://commonservicelocator.codeplex.com/"&gt;Common Service Locator&lt;/a&gt;&lt;/td&gt;
&lt;td width="561" valign="top"&gt;
&lt;ul&gt;
&lt;li&gt;Provides an adapter for working with MEF &lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="274" valign="top"&gt;&lt;a href="http://mefedmvvm.codeplex.com/"&gt;MefedMVVM&lt;/a&gt;&lt;/td&gt;
&lt;td width="560" valign="top"&gt;
&lt;ul&gt;
&lt;li&gt;MEF handles discovery and wire up of View Models both at runtime and design time &lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="275" valign="top"&gt;&lt;a href="http://phpmef.codeplex.com/"&gt;PHPMEF&lt;/a&gt;&lt;/td&gt;
&lt;td width="560" valign="top"&gt;
&lt;ul&gt;
&lt;li&gt;Yes, it&amp;rsquo;s possible, MEF for PHP :-) &lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="275" valign="top"&gt;&lt;a href="http://pocketmef.codeplex.com/"&gt;PocketMEF&lt;/a&gt;&lt;/td&gt;
&lt;td width="560" valign="top"&gt;
&lt;ul&gt;
&lt;li&gt;MEF for PocketPC &lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="275" valign="top"&gt;&lt;a href="http://www.mono-project.com/Main_Page"&gt;Mono&lt;/a&gt;&lt;/td&gt;
&lt;td width="560" valign="top"&gt;
&lt;ul&gt;
&lt;li&gt;Yes you heard it right, MEF 3.5 joins MVC2, System.Dynamic and OData client as part of the &lt;a href="http://www.mono-project.com/Release_Notes_Mono_2.8"&gt;mono&lt;/a&gt;. &lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;MEF V1 has been an awesome ride. The best part has been the fantastic &lt;a href="http://mef.codeplex.com/Thread/List.aspx"&gt;community&lt;/a&gt; that worked with us every step of the way giving us continual feedback and ensuring our success as well as our internal partners who pressed hard on us to keep us honest. Thank you for all your help and support!&lt;/p&gt;
&lt;p&gt;I also want to thank my team for the fantastic work they did. It was an amazing experience building MEF together!&lt;/p&gt;
&lt;p&gt;Sit tight, MEF v2 is on the way!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10019238" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category></item><item><title>Caveats about DeploymentCatalog</title><link>http://blogs.msdn.com/b/gblock/archive/2010/04/13/caveats-about-deploymentcatalog.aspx</link><pubDate>Tue, 13 Apr 2010 20:58:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9995416</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=9995416</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2010/04/13/caveats-about-deploymentcatalog.aspx#comments</comments><description>&lt;P&gt;The information below was wrapped up in my previous post on DeploymentCatalog. As a bunch of folks have been asking questions on this, it seems worth to rip out on it's own.&lt;/P&gt;
&lt;H1&gt;A few caveats about DeploymentCatalog.&lt;/H1&gt;
&lt;P&gt;DeploymentCatalog is a very exciting addition to the MEF box. There are a few caveats to it’s usage.&lt;/P&gt;
&lt;UL done1="370" done2="370"&gt;
&lt;LI done1="371" done2="371"&gt;Does not support Silverlight cached assemblies. (Otherwise known as TPEs). This means that DC will not download referenced assemblies that are packaged with the Silverlight caching infrastructure (.extmap). 
&lt;UL done1="372" done2="372"&gt;
&lt;LI&gt;This includes cached assemblies in the main xap. 
&lt;LI&gt;If the cached assemblies are referenced by the main app, they will be available to the assemblies in a downloaded XAP. 
&lt;LI&gt;You can create shared xaps which contain assemblies to be used by other xaps and download those with the DeploymentCatalog. As long as the shared xaps are downloaded first they will be available to others. 
&lt;LI&gt;If you do decide to use either of the previous approaches, be sure to set your references to “Copy Local = False” in your XAPs otherwise you will be embedding the shared assemblies again.Does not support localization. 
&lt;LI&gt;We are looking into cached assembly support in the future. &lt;/LI&gt;&lt;/UL&gt;
&lt;LI&gt;Does not support non-embedded resources. 
&lt;LI&gt;Local resource references in XAML also are not supported i.e. using pack URIs. You can programatically access embedded resources though. &lt;/LI&gt;&lt;/UL&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9995416" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/SL4/">SL4</category></item><item><title>View Model Locator – MEF Style</title><link>http://blogs.msdn.com/b/gblock/archive/2010/03/30/view-model-locator-mef-style.aspx</link><pubDate>Tue, 30 Mar 2010 09:19:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9987276</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=9987276</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2010/03/30/view-model-locator-mef-style.aspx#comments</comments><description>&lt;p&gt;At MIX, Laurent Bugnion and I had a brainstorming chat around improving on the &lt;a href="http://blog.galasoft.ch/archive/2010/03/16/whatrsquos-new-in-mvvm-light-v3.aspx"&gt;ViewModel locator&lt;/a&gt; “pattern” through the usage of a MEF generic locator that allows binding via indexer properties thus removing the need for a hard-coded locator. Tonite John Papa pinged me with a similar thought. Next thing you know we paired up for about 40 mins…and Voila, Success! &lt;/p&gt;  &lt;p&gt;Caveat: We hit a binding bug on SL4 that made it a little uglier than it needs to be through requiring a converter. Hopefully that will be fixed soon.&lt;/p&gt;  &lt;p&gt;Go read about it on &lt;a href="http://johnpapa.net/silverlight/simple-viewmodel-locator-for-mvvm-the-patients-have-left-the-asylum/"&gt;John’s blog!&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9987276" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/gblock/archive/tags/silverlight/">silverlight</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MVVM/">MVVM</category></item><item><title>Hamilton and I MEFFing it up on Silverlight TV</title><link>http://blogs.msdn.com/b/gblock/archive/2010/03/14/hamilton-and-i-meffing-it-up-on-silverlight-tv.aspx</link><pubDate>Sun, 14 Mar 2010 14:01:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9978301</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=9978301</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2010/03/14/hamilton-and-i-meffing-it-up-on-silverlight-tv.aspx#comments</comments><description>&lt;p&gt;&lt;img alt="Silverlight TV" src="http://ecn.channel9.msdn.com/o9/content/images/SilverlightTV_Bug.png" width="204" height="204" /&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Several weeks ago &lt;a href="http://hammett.castleproject.org/"&gt;Hamilton&lt;/a&gt; and I recorded several episodes with John Papa on the new &lt;a href="http://channel9.msdn.com/shows/silverlighttv/"&gt;Silverlight TV&lt;/a&gt; show on Channel 9. We did not one or two but FOUR episodes on Silverlight TV covering the basics as well as some more advanced cases like using metadata views and application partitioning. We also discussed when you should and should &lt;em&gt;not&lt;/em&gt; use MEF.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://channel9.msdn.com/shows/SilverlightTV/Silverlight-TV-Episode-4-3-Steps-to-MEF-Export-Import-Compose/"&gt;&lt;img alt="" src="http://ecn.channel9.msdn.com/o9/ch9/1/7/1/1/2/5/SLTVEpisode4MEF3Steps_85_ch9.png" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;h4&gt;&lt;a href="http://channel9.msdn.com/shows/SilverlightTV/Silverlight-TV-Episode-4-3-Steps-to-MEF-Export-Import-Compose/"&gt;Silverlight TV Episode 4: 3 Steps to MEF - Export, Import, Compose&lt;/a&gt;&lt;/h4&gt;  &lt;p&gt;&lt;a href="http://channel9.msdn.com/shows/SilverlightTV/Silverlight-TV-Episode-5-Using-Metadata-with-MEF/"&gt;&lt;img alt="" src="http://ecn.channel9.msdn.com/o9/ch9/1/9/6/3/2/5/SLTV005UsingMetadataWithMEF_85_ch9.png" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;h4&gt;&lt;a href="http://channel9.msdn.com/shows/SilverlightTV/Silverlight-TV-Episode-5-Using-Metadata-with-MEF/"&gt;Silverlight TV Episode 5: Using Metadata with MEF&lt;/a&gt;&lt;/h4&gt;  &lt;p&gt;   &lt;p&gt;&lt;a href="http://channel9.msdn.com/shows/SilverlightTV/Silverlight-TV-Episode-5-Using-Metadata-with-MEF/"&gt;&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;&lt;a href="http://channel9.msdn.com/shows/SilverlightTV/Silverlight-TV-7-When-and-Where-to-use-MEF/"&gt;&lt;img alt="" src="http://ecn.channel9.msdn.com/o9/ch9/9/2/2/6/2/5/SLTV007_85_ch9.png" /&gt;&lt;/a&gt;&lt;/p&gt;    &lt;h4&gt;&lt;a href="http://channel9.msdn.com/shows/SilverlightTV/Silverlight-TV-7-When-and-Where-to-use-MEF/"&gt;Silverlight TV 7: When and Where to use MEF&lt;/a&gt;&lt;/h4&gt;    &lt;p&gt;&lt;a href="http://channel9.msdn.com/shows/SilverlightTV/Silverlight-TV-11-Dynamically-Loading-XAPs-with-MEF/"&gt;&lt;img alt="" src="http://ecn.channel9.msdn.com/o9/ch9/3/3/2/6/2/5/SLTV008_85_ch9.png" /&gt;&lt;/a&gt;&lt;/p&gt;    &lt;h4&gt;&lt;a href="http://channel9.msdn.com/shows/SilverlightTV/Silverlight-TV-11-Dynamically-Loading-XAPs-with-MEF/"&gt;Silverlight TV 11: Dynamically Loading XAPs with MEF&lt;/a&gt;&lt;/h4&gt;    &lt;p&gt;It was great hanging out with John and exploring what you can do with this technology. Also, he’s an awesome host :-)&lt;/p&gt;    &lt;p&gt;If you want to get quickly ramped up on what MEF is and how you use it in Silverlight, go check them out!&lt;/p&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9978301" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category></item><item><title>Speaking at MIX!</title><link>http://blogs.msdn.com/b/gblock/archive/2010/03/11/speaking-at-mix.aspx</link><pubDate>Thu, 11 Mar 2010 10:18:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9976753</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=9976753</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2010/03/11/speaking-at-mix.aspx#comments</comments><description>&lt;p&gt;&lt;img alt="Love Fest" src="http://live.visitmix.com/Skins/MIX10/Styles/img/Mix10_LoveFest_brn_240.jpg" /&gt;&lt;/p&gt;  &lt;p&gt;Sunday I leave to Vegas to attend my first MIX. It’s exciting to finally attend this event that has such an air of mystique around it. I’ll be speaking on how you can use MEF in SL4 to partition your applications across many XAPs in a fairly seemless fashion. My talk is short, but it’s going to be very focused. If you’ve heard of MEF but are not sure what to do with it, or you know MEF and want to see some new ways it can help you make your Silverlight apps offer a better user experience, come to this talk.&lt;/p&gt;  &lt;h3&gt;&lt;a href="http://live.visitmix.com/MIX10/Sessions/CL52" mce_href="http://live.visitmix.com/MIX10/Sessions/CL52"&gt;Microsoft Silverlight Optimization and Extensibility with MEF&lt;/a&gt;&lt;/h3&gt;  &lt;p&gt;&lt;a href="http://live.visitmix.com/Speakers/Glenn-Block" mce_href="http://live.visitmix.com/Speakers/Glenn-Block"&gt;Glenn Block&lt;/a&gt; in Lagoon B on Tuesday at 3:35 PM&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Wouldn't it be nice if your team could add new features to your applications without all the headache they have to deal with today? With the Managed Extensiblity Framework (MEF) in 4.0 you can bolt your apps together dynamically on the fly. Adding new modules and features is as simple as deploying a new binary, you don't have to touch the existing code. You can even deploy those features as separate XAP files which can be loaded on-demand! Come to this talk for a quick tour of what MEF is, and learn how you can use it to beat out your competition.&lt;/p&gt;&lt;/blockquote&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9976753" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/Mix/">Mix</category></item><item><title>Speaking on “Ignite your coding”</title><link>http://blogs.msdn.com/b/gblock/archive/2010/03/08/speaking-on-ignite-your-coding.aspx</link><pubDate>Mon, 08 Mar 2010 22:39:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9975122</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=9975122</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2010/03/08/speaking-on-ignite-your-coding.aspx#comments</comments><description>&lt;p&gt;This Thursday, I’ll be joining &lt;a href="http://www.bristowe.com/"&gt;John Bristowe&lt;/a&gt; and &lt;a href="http://www.joeydevilla.com"&gt;Joey Devilla&lt;/a&gt; to talk about composite applications, patterns, MEF and anything else that comes up. I am flexible :-)&lt;/p&gt;  &lt;p&gt;This is a live webcast where questions will be taken in real time, so ask away! I am selfish but no slides or demo prep sounds great to me!&lt;/p&gt;  &lt;p&gt;Register here: &lt;a title="http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032439322&amp;amp;Culture=en-CA" href="http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032439322&amp;amp;Culture=en-CA"&gt;http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032439322&amp;amp;Culture=en-CA&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9975122" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/gblock/archive/tags/talks/">talks</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category></item><item><title>Building HelloMEF – Part V – Refactoring to ViewModel</title><link>http://blogs.msdn.com/b/gblock/archive/2010/03/08/building-hellomef-part-v-refactoring-to-viewmodel.aspx</link><pubDate>Mon, 08 Mar 2010 09:22:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9974664</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=9974664</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2010/03/08/building-hellomef-part-v-refactoring-to-viewmodel.aspx#comments</comments><description>&lt;p&gt;In the last post we migrated over to the new DeploymentCatalog. In this post we’ll look at refactoring the code to incorporate the MVVM pattern. Code from the last post is available &lt;a href="http://cid-f8b2fd72406fb218.skydrive.live.com/self.aspx/blog/Hello%20MEF/HelloMEF%5E_Part%5E_IV.zip"&gt;here&lt;/a&gt;&lt;/p&gt;  &lt;h1&gt;Why ViewModel?&lt;/h1&gt;  &lt;p&gt;There’s a ton of content out there in the blogosphere that discuss the virtues of separated presentation patterns including MVVM. I am not going to reiterate those here, but I will point you at &lt;a href="http://joshsmithonwpf.wordpress.com/"&gt;Josh Smith’s&lt;/a&gt; excellent MVVM article &lt;a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx"&gt;here&lt;/a&gt;. I’ll also shamelessly plug my own &lt;a href="http://blogs.msdn.com/blogs/glenn.block/archive/2009/08/03/the-spirit-of-mvvm-viewmodel-it-s-not-a-code-counting-exercise.aspx"&gt;post&lt;/a&gt; where I shared my own view on the essence of MVVM (which is based on views of others i respect). In short it’s about decoupling UI logic for better maintainability which includes making it easier for a graphic designer to style the UI.&lt;/p&gt;  &lt;p&gt;So where are the maintainability issues in the current “app”? Currently the main place is within MainPage.xaml.cs where you see the following code:&lt;/p&gt;  &lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; OnImportsSatisfied()
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;{
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    TopWidgets.Items.Clear();
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    BottomWidgets.Items.Clear();
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (var widget &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; Widgets)
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    {
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (widget.Metadata.Location == WidgetLocation.Top)
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;            TopWidgets.Items.Add(widget.Value);
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        &lt;span style="color: #0000ff"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (widget.Metadata.Location == WidgetLocation.Bottom)
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;            BottomWidgets.Items.Add(widget.Value);
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    }
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;}&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;The logic is simple yes, but it still has issues. &lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The main issue is that it is mixing UI business logic with the rendering. The logic which determines which item goes to Top to Bottom is a completely separate concern from how those items actually get displayed. Having the logic mixed in means&amp;#160; the code is very brittle where any slight change to the way the UI is rendered can break it. It also makes it difficult for a graphic designer from being able to change the look and feel of how the widgets are rendered. &lt;/li&gt;

  &lt;li&gt;The second issue is that I am imperatively adding elements to the UI rather than letting the rich DataBinding engine do the work it was designed for. This makes it difficult to re-skin the UI. &lt;/li&gt;

  &lt;li&gt;Third, in it’s current form, the logic is difficult to test, meaning if the logic breaks I am relying on QA / manual testing to pick it up. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;So ViewModel will fix all of this?&lt;/h2&gt;

&lt;p&gt;I say with confidence Yes! Using ViewModel will allow us to refactor the logic into a nice reusable and testable place. It will also gives us the decoupling we need to leverage the Silverlight rendering engine to it’s fullest. Along the way you will also see how MEF can help us with regards to implementing MVVM within our applications. We’ll introduce 2 view models to help us get there. When we’re done, our application will look like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/glenn.block/image_5F00_015C95DB.png"&gt;&lt;img title="image" border="0" alt="image" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/glenn.block/image_5F00_thumb_5F00_497A2BEA.png" width="419" height="186" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Pre-requisites.&lt;/h2&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;As with the last post, you’ll need our MEF codeplex bits for following along. If you start where we left off in the last post you’ll be fine. You’ll also need a copy of &lt;a href="http://galasoft.ch/"&gt;Laurent Bugnion’s&lt;/a&gt; awesome “Glenn Block approved” :-) MVVM Light, which you can get &lt;a href="http://mvvmlight.codeplex.com/"&gt;here&lt;/a&gt; and which we’ll be using for it’s ICommand default implementation.&lt;/p&gt;

&lt;h1&gt;&lt;/h1&gt;

&lt;h1&gt;Let the refactoring begin!&lt;/h1&gt;

&lt;p&gt;First we’ll go create our ViewModel. We’ll start off with an empty class. Go add a new MainPageViewModel.cs class to the HelloMEF project. Leave it empty for now. Now let’s refactor MainPage to have MEF deliver it’s ViewModel. There’s a lot of religous debate in the community around View First vs ViewModel First construction of the UI. I am going to use View first, period, and no that’s not open for debate :-) &lt;/p&gt;

&lt;h2&gt;Refactoring MainPageView&lt;/h2&gt;

&lt;p&gt;What we’re going to do is rip out the logic in MainPageView, and replace it with importing it’s ViewModel and setting it to the DataContext. Replace MainPageView.xaml.cs the following:&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; partial &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; MainPage : UserControl
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;{
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; MainPage()
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    {
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        InitializeComponent();
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        CompositionInitializer.SatisfyImports(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;);
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.DataContext = ViewModel;
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    }
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    [Import]
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; MainPageViewModel ViewModel { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;}
&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;I think you’ll agree this code is a lot cleaner. My UI is now completely decoupled from any UI “logic”. &lt;/p&gt;

&lt;p&gt;But WAIT, there IS code in the code behind, and the ViewModel police are probably on their way to my house. My answer when they show up is “it’s simple wiring logic, so WHAT!” That being said you can certainly look at ways to remove to right that small amount of code, but I am not losing any sleep over it. :-)&lt;/p&gt;

&lt;p&gt;Notice I am not using constructor injection rather I am importing my ViewModel through a property. The reason is because I am allowing XAML to create my view rather than having MEF create it for me. SatisfyImports is then called to tell MEF to inject the view. It removes one level of mental gymnastics which in particular manifests itself when you start dealing with nested Views and ViewModels. If you’ve dealt with manually assembling Composite UIs / using regions, you know what I mean. It also allows this view to be more designer friendly as I can configure a design time VM to show up. That is for another post.&lt;/p&gt;

&lt;h2&gt;Fleshing out MainPage’s ViewModel&lt;/h2&gt;

&lt;p&gt;Now we’re go and finish the empty MainPageViewModel we created earlier. We’re not just going to move the code from the view into the model as we’d just be moving the problem. Instead we’re going to get rid of the imperative code and allow the view to render itself on the model through binding. We’ll still need to leverage MEF’s metadata and ensure that the widgets go in the right “place” wherever that happens to be. This raises a question as to how as previously there was a single collection of widgets that we imported into MainPage before and which we manually walked. The solution I chose was to create collection properties for each set of widgets. That allows the view to render it any way it wishes and decouples from the ItemsControls that were used previously.&lt;/p&gt;

&lt;p&gt;Below is the code: &lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;[Export]
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; MainPageViewModel : IPartImportsSatisfiedNotification, 
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    INotifyPropertyChanged
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;{
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    [ImportMany(AllowRecomposition = &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;)]
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Lazy&amp;lt;UserControl, IWidgetMetadata&amp;gt;[] Widgets { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IEnumerable&amp;lt;UserControl&amp;gt; TopWidgets { &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;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IEnumerable&amp;lt;UserControl&amp;gt; BottomWidgets { &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;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; OnImportsSatisfied()
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    {
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        TopWidgets = Widgets.Where(w =&amp;gt; w.Metadata.Location == 
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;            WidgetLocation.Top).Select(i =&amp;gt; i.Value);
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        BottomWidgets = Widgets.Where(w =&amp;gt; w.Metadata.Location == 
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;            WidgetLocation.Bottom).Select(i =&amp;gt; i.Value);
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        OnPropertyChanged(&amp;quot;&lt;span style="color: #8b0000"&gt;TopWidgets&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        OnPropertyChanged(&amp;quot;&lt;span style="color: #8b0000"&gt;BottomWidgets&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    }
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;event&lt;/span&gt; PropertyChangedEventHandler PropertyChanged;
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; OnPropertyChanged(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; property)
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    {
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        var handler = PropertyChanged;
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (handler != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;            PropertyChanged(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; PropertyChangedEventArgs(
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;                property));
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    }
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;}&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;MainPageViewModel implements IPartImportSatisfiedNotification as the view did previously. It also implements INotifyPropertyChanged in order to tell the view whenever it is recomposed. It exports itself in order to make itself available to be imported by the view. MainPageViewModel imports all the Widgets as did the view, but it also exposes two different properties TopWidgets and BottomWidgets. Whenever recompostion occurs, OnImportsSatisfied is called which updates these properties with their own filtered collections by using a LINQ query with the appropriate metadata filter. It then raises PropertyChanged in order to update the UI. This really shines through the power of metadata and metadata views in MEF!&lt;/p&gt;

&lt;h2&gt;Updating the bindings&lt;/h2&gt;

&lt;p&gt;The last thing we need to do to get the ViewModel wired is to update the view to bind the widget ItemControls to the appropriate properties. Replace the StackPanel in MainPage.xaml.&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;StackPanel&lt;/span&gt; &lt;span style="color: #ff0000"&gt;x&lt;/span&gt;:&lt;span style="color: #ff0000"&gt;Name&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;LayoutRoot&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Background&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;Black&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Border&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Background&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;Cyan&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;ItemsControl&lt;/span&gt; &lt;span style="color: #ff0000"&gt;x&lt;/span&gt;:&lt;span style="color: #ff0000"&gt;Name&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;TopWidgets&amp;quot;&lt;/span&gt; 
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;            &lt;span style="color: #ff0000"&gt;ItemsSource&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;{Binding TopWidgets}&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Height&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;Auto&amp;quot;&lt;/span&gt; 
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;            &lt;span style="color: #ff0000"&gt;FontSize&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;30&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;ItemsControl&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Border&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Border&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Background&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;Yellow&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;ItemsControl&lt;/span&gt; &lt;span style="color: #ff0000"&gt;x&lt;/span&gt;:&lt;span style="color: #ff0000"&gt;Name&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;BottomWidgets&amp;quot;&lt;/span&gt; 
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;            &lt;span style="color: #ff0000"&gt;ItemsSource&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;{Binding BottomWidgets}&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Height&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;Auto&amp;quot;&lt;/span&gt; 
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;            &lt;span style="color: #ff0000"&gt;FontSize&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;30&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;ItemsControl&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Border&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;StackPanel&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;h2&gt;We’re almost there&lt;/h2&gt;

&lt;p&gt;Build and execute the app. You should see that everything works as before only now our new ViewModel is in place. You can now easily go and and reformat the UI without impacting the underlying ViewModel at all. We could stop here as we’ve already made a good leap in improving the maintenance of our app. We won’t though, there’s one more culprit we can refactor. Widget1.&lt;/p&gt;

&lt;h2&gt;What’s wrong with Widget1?&lt;/h2&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;[ExportWidget(Location = WidgetLocation.Top)]
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; partial &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Widget1 : UserControl
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;{
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    [Import]
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IDeploymentCatalogService CatalogService { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Widget1()
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    {
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        InitializeComponent();
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        Button.Click += &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; RoutedEventHandler(Button_Click);
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    }
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Button_Click(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    {
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        CatalogService.AddXap(&amp;quot;&lt;span style="color: #8b0000"&gt;HelloMEF.Extensions.xap&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    }
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;}&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Widget1 contains logic which calls the DeploymentCatalogService to download a XAP, it’s not horrible but it has some of the same problems as MainPage did.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;It tightly couples the download logic to the UI. This means if you refactor the UI there is a decent chance it will break. 
    &lt;ul&gt;
      &lt;li&gt;Downloading widgets is a completely non-UI concern which does not belong in the view. &lt;/li&gt;

      &lt;li&gt;It means a designer can’t change from using a button without modifying the code. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;The logic is not reusable, you can’t reuse it across multiple views. This may or may not matter depending on the specific case. &lt;/li&gt;

  &lt;li&gt;As small as it is, it is hard to test. That means if someone breaks the code and it no longer works, the only way to pick it up is through QA/automated UI testing. We want to pick up as many bugs as we can up stream rather than waiting for them to be caught. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Refactoring Widget1 to use a ViewModel.&lt;/h2&gt;

&lt;p&gt;Widget1 can be refactored to use it’s own ViewModel. We can then refactor the logic out of the code behind and move it into the model leveraging Silverlight 4’s new commanding support, Laurent’s MVVM light library, and a little MEF to make it more maintainable.&lt;/p&gt;

&lt;p&gt;We’ll do this again with the ViewFirst approach :-) This time we don’t need to use CompositionInitializer though as our ViewModel will get discovered by the catalog. Add a new Widget1ViewModel.cs file to the HelloMEF project. Second add a reference to GalaSoft.MVVM light (in your “ProgramFiles\Laurent Bugnion (Galasoft)\Mvvm Light Toolkit\Binaries\Silverlight” folder). Now paste in the following code to Widget1ViewModel.cs.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.ComponentModel.Composition;
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Windows.Input;
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; GalaSoft.MvvmLight.Command;
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;namespace&lt;/span&gt; HelloMEF
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;{
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    [Export]
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Widget1ViewModel
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    {
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Widget1ViewModel()
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        {
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;            Download = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; RelayCommand(() =&amp;gt; 
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;                CatalogService.AddXap(&amp;quot;&lt;span style="color: #8b0000"&gt;HelloMEF.Extensions.xap&lt;/span&gt;&amp;quot;));
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        }
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        [Import]
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IDeploymentCatalogService CatalogService { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ICommand Download { &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;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    }
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;}&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Widget1ViewModel is an export similar to MainPageViewModel. It imports IDeploymentCatalogService in order to allow downloading XAPs. It then exposes a Download command which is implemented using MVVM Lights’ (contributed by Josh Smith) RelayCommand to download the extensions. &lt;/p&gt;

&lt;h2&gt;Updating Widget1View&lt;/h2&gt;

&lt;p&gt;Once we have that in place we can clean up the Widget1 view. Paste the following into Widget1.xaml.cs.&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;[ExportWidget(Location=WidgetLocation.Top)]
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; partial &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Widget1 : UserControl
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;{
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Widget1()
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    {
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        InitializeComponent();
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    }
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    [Import]
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Widget1ViewModel ViewModel
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    {
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;{ &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; (Widget1ViewModel) &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.DataContext;}
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        &lt;span style="color: #0000ff"&gt;set&lt;/span&gt; { &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.DataContext = &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;;}
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    }
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;}
&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;First I removed the previous UI logic. Then I added an import of the MainPageViewModel and set it to the DataContext. Constructor injection was also an option through usage of our ImportingConstructor attribute. I chose not to for consistency and in order to not increase the concept count. Feel free to use either approach though.&lt;/p&gt;

&lt;p&gt;Now that we’ve updated the code behind we can update the XAML to bind to the new DownloadCommand. Replace the Grid element in Widget1.xaml with the following.&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Grid&lt;/span&gt; &lt;span style="color: #ff0000"&gt;x&lt;/span&gt;:&lt;span style="color: #ff0000"&gt;Name&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;LayoutRoot&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Height&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;Auto&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Button&lt;/span&gt; &lt;span style="color: #ff0000"&gt;x&lt;/span&gt;:&lt;span style="color: #ff0000"&gt;Name&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;Button&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Content&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;Hello MEF!&amp;quot;&lt;/span&gt; 
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;        &lt;span style="color: #ff0000"&gt;Command&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;{Binding Download}&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Width&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;300&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Height&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;100&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Button&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Grid&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;h2&gt;Run the app!&lt;/h2&gt;

&lt;p&gt;Build and run the application, press the top button and you should see a screen that is quite familiar by now :-)&lt;/p&gt;

&lt;p&gt;&lt;a href="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/glenn.block/image_5F00_07A568C4.png"&gt;&lt;img title="image" border="0" alt="image" src="http://codebetter.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/glenn.block/image_5F00_thumb_5F00_69E3DE04.png" width="487" height="348" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We’re done.&lt;/p&gt;

&lt;h1&gt;What did we gain?&lt;/h1&gt;

&lt;p&gt;You might be sitting there thinking wow that was fun, but what did it buy me? Here’s a quite summary:'&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;MainPage and Widget1 are completely decoupled from the UI logic. &lt;/li&gt;

  &lt;li&gt;MainPageViewModel and Widget1ViewModel are easily testable. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both of which translate into less pain for us as our app evolves.&lt;/p&gt;

&lt;h1&gt;How did MEF help?&lt;/h1&gt;

&lt;p&gt;MEF enabled us to put the decoupled pieces back together. To be fair, we could have used other mechanisms. However MEF offered a nice solution in the box, which was convenient as our app was already using it :-) MEF also provided us CompositionInitializer which made it very easy to compose our UI parts without having to jump through a lot of hoops.&lt;/p&gt;

&lt;h1&gt;What’s next?&lt;/h1&gt;

&lt;p&gt;In this post I mentioned testing several times though notice I did not show ANY tests. That was deliberate :-) The reason is because I wanted you to see the value of ViewModel beyond the simple testability aspects, I wanted you to focus on how it improved our code whether we were testing it our not. In the next post we’ll look at how we can test it.&lt;/p&gt;

&lt;p&gt;As always, code is attached.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9974664" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/viewmodel/">viewmodel</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/SL4/">SL4</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/SL/">SL</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/HelloMEF/">HelloMEF</category></item><item><title>Building Hello MEF – Part IV – DeploymentCatalog</title><link>http://blogs.msdn.com/b/gblock/archive/2010/03/08/building-hello-mef-part-iv-deploymentcatalog.aspx</link><pubDate>Mon, 08 Mar 2010 01:54:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9974533</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=9974533</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2010/03/08/building-hello-mef-part-iv-deploymentcatalog.aspx#comments</comments><description>&lt;P&gt;Continuing on with the series. In &lt;A href="http://blogs.msdn.com/gblock/archive/2009/12/15/building-hello-mef-part-iii-xap-partitioning-with-the-host-s-permission-and-the-sweetness-of-recomposition.aspx" mce_href="http://blogs.msdn.com/gblock/archive/2009/12/15/building-hello-mef-part-iii-xap-partitioning-with-the-host-s-permission-and-the-sweetness-of-recomposition.aspx"&gt;part III&lt;/A&gt; we introduced the concept of application partitioning through the use of the PackageCatalog which ships in the Silverlight toolkit. In this post we’ll take a look at a new API known as the DeploymentCatalog which ships in the box! We also look at some changes to our hosting APIs. If you are following along, the completed code from that post is available &lt;A href="http://cid-f8b2fd72406fb218.skydrive.live.com/self.aspx/blog/Hello%20MEF/HelloMEF%5E_Part%5E_III%20Completed.zip" mce_href="http://cid-f8b2fd72406fb218.skydrive.live.com/self.aspx/blog/Hello%20MEF/HelloMEF%5E_Part%5E_III%20Completed.zip"&gt;here&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Note: Instead of using the MEF binaries that ship as part of SL4 beta, we’re going to use our newer bits available on Codeplex from this point forward. It is very likely these same APIs will be available in the box when SL4 ships ;-)&lt;/P&gt;
&lt;H1&gt;DeploymentCatalog&lt;/H1&gt;
&lt;P&gt;With our latest codeplex drop we introduced a new API called DeploymentCatalog. DeploymentCatalog is basically a redesign of the catalog that we included in the toolkit. As part of the redesign we address some thread-safety issues as well as made the catalog more robust.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIVDeploymentCataloga_D5DA/image_4.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIVDeploymentCataloga_D5DA/image_4.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIVDeploymentCataloga_D5DA/image_thumb_1.png" width=268 height=305 mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIVDeploymentCataloga_D5DA/image_thumb_1.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;DeploymentCatalog (DC) is used for dynamically downloading MEF catalogs in an async fashion. To use it you call the DownloadAsync method passing in a uri with the default overload accepting a relative uri. The uri should point to a XAP which contains MEF parts. Once you initiate the download, DC will start the download. Upon completion DC will rip open the XAP, load all the assemblies within and add any attributed parts to the catalog. You’ll notice that unlike PackageCatalog which holds a collection of packages for multiple XAPs, each DC corresponds to a single XAP. &lt;/P&gt;
&lt;P&gt;Below is a snippet of how to use it.&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;var catalog = &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; DeploymentCatalog("&lt;SPAN style="COLOR: #8b0000"&gt;Extensions.xap&lt;/SPAN&gt;");
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;catalog.DownloadAsync();&lt;/PRE&gt;&lt;/PRE&gt;
&lt;H2&gt;Passing deployment catalogs to PartInitializer&lt;/H2&gt;
&lt;P&gt;In the previous bits, CompositionHost.InitializerContainer was called in order to pass a container configured with a PackageCatalog which PartInitializer could use.In the new bits PartInitializer has been renamed to CompositionInitializer (more on that later). Additionally we’ve renamed InitializeContainer to Initializer and added a new convenience overload which accepts a param array of catalogs. When you use this overload you need to decide if you want the default XAP’s parts to be added. To do this pass in a DeploymentCatalog created with the default constructor such as the example below:&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;CompositionHost.Initialize(&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; DeploymentCatalog(), catalog);&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;Using the catalog from the previous example this will add all of the parts from the current XAP as well as any parts that are discovered in “Extensions.xap”.&lt;/P&gt;
&lt;H2&gt;Recomposition in DeploymentCatalog&lt;/H2&gt;
&lt;P&gt;Each DC downloads in an asynchronous manner. Because DC is recomposable (implements INotifyComposablePartCatalogChanged), it will notify whenever the download completes. This means that you are not required to subscribe to the DownloadCompleted event, as because the catalog is recomposable, it will force the container to recompose upon the receipt of parts. In the previous example, adding a catalog to DC does not mean that it’s parts have been downloaded yet. However once those parts are downloaded, the container will recompose. &lt;/P&gt;
&lt;H2&gt;Tracking completion, errors and progress.&lt;/H2&gt;
&lt;P&gt;You can, and we recommend you do track download completion and errors. Whenever a download completes, the catalog will raise a “DownloadCompleted” event. If the download fails for some reason, then DownloadCompleted will be fired setting the Error to the download error. You can also track overall progress of the download by subscribing to the DownloadProgressChanged event. Finally if your application logic requires it you can even cancel the download by calling CancelAsync.&lt;/P&gt;
&lt;P&gt;For example see the snippet below.&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; DownloadCatalog(&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; uri) {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    var catalog = &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; DeploymentCatalog(uri);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    catalog.DownloadCompleted += &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; EventHandler&amp;lt;AsyncCompletedEventArgs&amp;gt;(DownloadCompleted);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    catalog.DownloadProgress += &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; EventHandler&amp;lt;DownloadProgressChangedEventArgs&amp;gt;(DownloadProgressChanged);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    catalog.DownloadAsync();
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    &lt;SPAN style="COLOR: #008000"&gt;//cancel the catalog download due to a timeout&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    catalog.CancelAsync();
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;}
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; DownloadCompleted(&lt;SPAN style="COLOR: #0000ff"&gt;object&lt;/SPAN&gt; sender, AsyncCompletedEventArgs e)
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;{
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (e.Error != &lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;)
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;         &lt;SPAN style="COLOR: #0000ff"&gt;throw&lt;/SPAN&gt; e.Error;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;}
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; catalog_DownloadProgressChanged(&lt;SPAN style="COLOR: #0000ff"&gt;object&lt;/SPAN&gt; sender, DownloadProgressChangedEventArgs e)
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;{
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;  &lt;SPAN style="COLOR: #008000"&gt;//progress logic here&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;}
&lt;/PRE&gt;&lt;/PRE&gt;
&lt;H2&gt;Allowing parts to download XAPs.&lt;/H2&gt;
&lt;P&gt;In the last post we added the PackageCatalog directly to the container in order to allow parts to import it. A cleaner approach is to encapsulate the catalog management into a nice service that other parts can depend on. This allows the host to have more fine-grained control over how and what enters the container. In the sections that follow you’ll see I’ve introduced a DeploymentCatalogService for this purpose.&lt;/P&gt;
&lt;H1&gt;Migrating the code&lt;/H1&gt;
&lt;P&gt;OK, let’s start migrating the code to use the new DeploymentCatalog apis. Again the starter code is available &lt;A href="http://cid-f8b2fd72406fb218.skydrive.live.com/self.aspx/blog/Hello%20MEF/HelloMEF%5E_Part%5E_III%20Completed.zip" mce_href="http://cid-f8b2fd72406fb218.skydrive.live.com/self.aspx/blog/Hello%20MEF/HelloMEF%5E_Part%5E_III%20Completed.zip"&gt;here&lt;/A&gt;&lt;/P&gt;
&lt;H2&gt;Download MEF bits&lt;/H2&gt;
&lt;P&gt;First we need to go download the latest MEF bits from Codeplex from &lt;A href="http://mef.codeplex.com/releases/view/40606" mce_href="http://mef.codeplex.com/releases/view/40606"&gt;here&lt;/A&gt;. Extract the zip (make sure to unblock all files if you are letting windows extract it for you). Next open the HelloMEF solution in Visual Studio and go and remove all references to SystemComponentModel.Composition.dll, System ComponentModel.Composition.Initialization.dll, and SystemComponentModel.Composition.Packaging.Toolkit.dll from each project.&lt;/P&gt;
&lt;H2&gt;Fix MEF References&lt;/H2&gt;
&lt;P&gt;Now go add references to the newer binaries located in the .\bin\SL folder. Here are the projects and references.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;HelloMEF –&amp;gt; Add System.ComponentModel.Composition.dll and System.ComponentModel.Composition.Initialization.dll. &lt;/LI&gt;
&lt;LI&gt;HelloMEF.Contracts –&amp;gt; Add System.ComponentModel.Composition.dll. &lt;/LI&gt;
&lt;LI&gt;HelloMEF.Extensions –&amp;gt; Add System.ComponentModel.Composition.dll. &lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Introducing DeploymentCatalog service.&lt;/H2&gt;
&lt;P&gt;In order to handle initializing the host as well allowing other parts to download, we’ll create DeploymentCatalogService.&lt;/P&gt;
&lt;P&gt;First add a new interface to HelloMEF.Contracts using the following code.&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;interface&lt;/SPAN&gt; IDeploymentCatalogService
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;{
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; AddXap(&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; uri, Action&amp;lt;AsyncCompletedEventArgs&amp;gt; completedAction = &lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; RemoveXap(&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; uri);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;P&gt;}&lt;/P&gt;&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;Next add a new DeploymentCatalogService to the HelloMEF project.&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;[Export(&lt;SPAN style="COLOR: #0000ff"&gt;typeof&lt;/SPAN&gt;(IDeploymentCatalogService))]
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; DeploymentCatalogService : IDeploymentCatalogService
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;{
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;static&lt;/SPAN&gt; AggregateCatalog _aggregateCatalog;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    Dictionary&amp;lt;&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt;, DeploymentCatalog&amp;gt; _catalogs;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; DeploymentCatalogService()
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;        _catalogs = &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; Dictionary&amp;lt;&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt;, DeploymentCatalog&amp;gt;();
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; Initialize()
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;        _aggregateCatalog = &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; AggregateCatalog();
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;        _aggregateCatalog.Catalogs.Add(&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; DeploymentCatalog());
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;        CompositionHost.Initialize(_aggregateCatalog);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; AddXap(&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; uri, Action&amp;lt;AsyncCompletedEventArgs&amp;gt; completedAction = &lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt; )
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;        DeploymentCatalog catalog;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (!_catalogs.TryGetValue(uri, &lt;SPAN style="COLOR: #0000ff"&gt;out&lt;/SPAN&gt; catalog))
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;        {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;            catalog = &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; DeploymentCatalog(uri);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;            &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (completedAction != &lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;)
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;                catalog.DownloadCompleted += (s, e) =&amp;gt; completedAction(e);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;            &lt;SPAN style="COLOR: #0000ff"&gt;else&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;                catalog.DownloadCompleted += &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; EventHandler&amp;lt;System.ComponentModel.AsyncCompletedEventArgs&amp;gt;(catalog_DownloadCompleted);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;            catalog.DownloadAsync();
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;            _catalogs[uri] = catalog;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;        }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;        _aggregateCatalog.Catalogs.Add(catalog);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; catalog_DownloadCompleted(&lt;SPAN style="COLOR: #0000ff"&gt;object&lt;/SPAN&gt; sender, System.ComponentModel.AsyncCompletedEventArgs e)
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (e.Error != &lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;)
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;            &lt;SPAN style="COLOR: #0000ff"&gt;throw&lt;/SPAN&gt; e.Error;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; RemoveXap(&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; uri)
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;        DeploymentCatalog catalog;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (_catalogs.TryGetValue(uri, &lt;SPAN style="COLOR: #0000ff"&gt;out&lt;/SPAN&gt; catalog))
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;        {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;            _aggregateCatalog.Catalogs.Remove(catalog);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;        }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;}
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;DeploymentCatalogService handles adding / removal of DeploymentCatalogs. It also exposes a static Initialize method which handles setting up the host to allow dynamic download. This method creates an aggregate catalog, and adds the parts in the default XAP to it. It then calls CompositionHost.Initialize to pass that catalog to the host. Once the host has been configured, any catalogs added to the aggregate will then automatically show up in the host.&lt;/P&gt;
&lt;P&gt;Note: DeploymentCatalogService is simply a helper, it is &lt;STRONG&gt;NOT&lt;/STRONG&gt; required in order to use DeploymentCatalog.&lt;/P&gt;
&lt;H2&gt;Initializing the host&lt;/H2&gt;
&lt;P&gt;Now that DeploymentCatalogService is added, go remove the old Initialization code which used PackageCatalog from the App class and have it call Initialize. &lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&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, StartupEventArgs e)
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;{
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    DeploymentCatalogService.Initialize();
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;.RootVisual = &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; MainPage();
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;}
&lt;/PRE&gt;&lt;/PRE&gt;
&lt;H2&gt;Refactoring Widget1 to use DeploymentCatalogService&lt;/H2&gt;
&lt;P&gt;Next go change Widget1.xaml.cs replacing the importing PackageCatalog with IDeploymentCatalogService.&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;[ExportWidget(Location=WidgetLocation.Top)]
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; partial &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; Widget1 : UserControl
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;{
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    [Import]
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; IDeploymentCatalogService CatalogService { &lt;SPAN style="COLOR: #0000ff"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: #0000ff"&gt;set&lt;/SPAN&gt;; }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; Widget1()
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;        InitializeComponent();
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;        Button.Click += &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; RoutedEventHandler(Button_Click);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; Button_Click(&lt;SPAN style="COLOR: #0000ff"&gt;object&lt;/SPAN&gt; sender, RoutedEventArgs e)
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;        CatalogService.AddXap("&lt;SPAN style="COLOR: #8b0000"&gt;HelloMEF.Extensions.xap&lt;/SPAN&gt;");
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;    }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;}
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 12px"&gt;&lt;/PRE&gt;&lt;/PRE&gt;
&lt;H2&gt;Build it!&lt;/H2&gt;
&lt;P&gt;Go build the project and run it. Press the top “Hello MEF” button and you should see a new green part appear which was dynamically downloaded through the new DeploymentCatalog.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIVDeploymentCataloga_D5DA/image_6.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIVDeploymentCataloga_D5DA/image_6.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIVDeploymentCataloga_D5DA/image_thumb_2.png" width=487 height=348 mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIVDeploymentCataloga_D5DA/image_thumb_2.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;H1&gt;A few caveats about DeploymentCatalog.&lt;/H1&gt;
&lt;P&gt;DeploymentCatalog is a very exciting addition to the MEF box. There are a few caveats to it’s usage.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Does not support Silverlight cached assemblies. (Otherwise known as TPEs). This means that DC will not download referenced assemblies that are packaged with the Silverlight caching infrastructure (.extmap). 
&lt;UL&gt;
&lt;LI&gt;This includes cached assemblies in the main xap. &lt;/LI&gt;
&lt;LI&gt;If the cached assemblies are referenced by the main app, they will be available to the assemblies in a downloaded XAP. &lt;/LI&gt;
&lt;LI&gt;You can create shared xaps which contain assemblies to be used by other xaps and download those with the DeploymentCatalog. As long as the shared xaps are downloaded first they will be available to others. &lt;/LI&gt;
&lt;LI&gt;If you do decide to use either of the previous approaches, be sure to set your references to “Copy Local = False” in your XAPs otherwise you will be embedding the shared assemblies again.Does not support localization. &lt;/LI&gt;
&lt;LI&gt;We are looking into cached assembly support in the future. &lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;
&lt;LI&gt;Does not support non-embedded resources. &lt;/LI&gt;
&lt;LI&gt;Local resource references in XAML also are not supported i.e. using pack URIs. You can programatically access embedded resources though. &lt;/LI&gt;&lt;/UL&gt;
&lt;H1&gt;Summary&lt;/H1&gt;
&lt;P&gt;Used properly, DeploymentCatalog is a great way to partition your Silverlight applications in order to improve startup time as well as allowing third-parties to extend your Silverlight applications.&lt;/P&gt;
&lt;P&gt;Completed code is attached.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9974533" width="1" height="1"&gt;</description><enclosure url="http://cid-f8b2fd72406fb218.skydrive.live.com/self.aspx/blog/Hello%20MEF/HelloMEF%5E_Part%5E_IV.zip" length="54521" type="application/xhtml+xml; charset=utf-8" /><category domain="http://blogs.msdn.com/b/gblock/archive/tags/silverlight/">silverlight</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/SL4/">SL4</category></item><item><title>Hosting MEF within application and libraries.</title><link>http://blogs.msdn.com/b/gblock/archive/2010/02/01/hosting-mef-within-application-and-libraries.aspx</link><pubDate>Mon, 01 Feb 2010 09:51:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9956233</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=9956233</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2010/02/01/hosting-mef-within-application-and-libraries.aspx#comments</comments><description>&lt;p&gt;As you start to work with MEF one of the fundamental decisions you have to make is how MEF will be hosted. There are several factors which will impact your decision including whether you are building on Silverlight or the Desktop and whether you are building an application or a library.&lt;/p&gt;  &lt;p&gt;The first concern you hit around MEF is how and where composition starts. MEF parts at the end of the day are simply classes (in our attributed model). Simply adding an Export attribute to a class does not cause it to get composed as something has to start the composition. That responsibility falls on the CompositionContainer. It uses one or more catalogs to discover the available parts in the system. Once the container is created, composition can begin. But it doesn’t begin until the container is asked to do work. This normally happens in the entry point of the application or application root, which in a WPF/Silverlight application would be the Application class.&lt;/p&gt;  &lt;p&gt;There are several approaches to composition in MEF.&lt;/p&gt;  &lt;h1&gt;Composing an existing part&lt;/h1&gt;  &lt;p&gt;The first approach is to pass an existing instance to MEF and ask it to compose it. The advantage of this approach is that it lets you declare imports on the existing application instance.&lt;/p&gt;  &lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;[Import]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Window MainWindow { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; App_Startup(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender, StartupEventArgs e)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;{
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    var catalog = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; AssemblyCatalog(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(App).Assembly);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    var container = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; CompositionContainer(catalog);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    container.ComposeParts(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;base&lt;/span&gt;.MainWindow = Window;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    MainWindow.Show();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}
&lt;/pre&gt;&lt;/pre&gt;

&lt;h1&gt;Pulling on the container for an export&lt;/h1&gt;

&lt;p&gt;In this approach you pull on the container and ask MEF to create a part.&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; App_Startup(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender, StartupEventArgs e)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;{
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    var catalog = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; AssemblyCatalog(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt; (App).Assembly);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    var container = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; CompositionContainer(catalog);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.MainWindow = container.GetExportedValue&amp;lt;Window&amp;gt;();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    MainWindow.Show();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;In the startup method above I am creating a catalog with the current assembly, adding it to a container and then querying for the MainWindow. During the querying, MEF discovers the MainWindow (assuming it was exported) &lt;/p&gt;

&lt;h1&gt;Creating a standardized class for starting composition&lt;/h1&gt;

&lt;p&gt;In the previous examples, bootstrapping code is introduced into the startup of the application. This kind of approach works well for client / server applications that have a centralized initialization point. In the previous case the initialization code is very specific. With some refactoring I can create a standardized initialization mechanism such as CompositionStarter below which can be reused across different UI mediums including on the server.&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; CompositionStarter&amp;lt;TParams&amp;gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;{
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; CompositionContainer _container;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; CompositionStarter(&lt;span style="color: #0000ff"&gt;params&lt;/span&gt; ComposablePartCatalog[] catalogs)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        var aggregate = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; AggregateCatalog(catalogs);&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        _container = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; CompositionContainer(aggregate);&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; CompositionStarter(CompositionContainer container)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (container == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ArgumentNullException(&amp;quot;&lt;span style="color: #8b0000"&gt;container&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        _container = container;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; CompositionStarter()
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        _container = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; CompositionContainer(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; AssemblyCatalog(Assembly.GetCallingAssembly()));
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Start(TParams parameters)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        var startables = _container.GetExportedValues&amp;lt;IStartable&amp;lt;TParams&amp;gt;&amp;gt;();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt;(var startable &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; startables)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            startable.Start(parameters);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Start()
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        Start(&lt;span style="color: #0000ff"&gt;default&lt;/span&gt;(TParams));
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Stop()
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (_container == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; InvalidOperationException(&amp;quot;&lt;span style="color: #8b0000"&gt;Stop can only be called once&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        _container.Dispose();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        _container = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;    
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;[InheritedExport]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; IStartable&amp;lt;TParams&amp;gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;{
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Start(TParams parameters);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;CompositionStarter&amp;lt;TParam&amp;gt; creates a container (or uses the one that you pass in). On the Start method it pulls all IStartable&amp;lt;TParam&amp;gt; exports from the container and invokes their Start methods passing in the parameters. With CompositionStarter in place, I can now introduce a WpfStartable and move the code for importing and displaying the window there.&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; WpfStartable : IStartable&amp;lt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&amp;gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;{
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    [Import]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Window Window { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Start(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; parameters)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        Application.Current.MainWindow = Window;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        Window.Show();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;The app class can then be updated to use the following starter code:&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; App_Startup(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender, StartupEventArgs e)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;{
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    var starter = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; CompositionStarter&amp;lt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&amp;gt;();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    starter.Start();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;The code is no longer app-specific, nor is it specific to a particular UI platform. The same code pattern also works for a Silverlight application, a WinForms application or a console app.&lt;/p&gt;

&lt;p&gt;Notice that CompositionStarter pulls multiple IStartable implementations. This allows me to introduce other pluggable startup operations. &lt;/p&gt;

&lt;h3&gt;&lt;/h3&gt;

&lt;h3&gt;A note about Silverlight and PartInitializer&lt;/h3&gt;

&lt;p&gt;In Silverlight we have introduced PartInitializer which automatically bootstraps a shared container and composes an existing instance. This is primarily for usage in places such as XAML where objects are created by the XAML parser, but need to get composed. PartInitializer may be sufficient for your startup needs in Silverlight so if it works, don’t feel compelled that must use CompositionStarter, you don’t.&lt;/p&gt;

&lt;p&gt;You may want to combine the two approaches. The benefits of doing this are you could have CompositionStarter bootstrap your app, while still using PartInitializer within XAML elements that need composition.&lt;/p&gt;

&lt;p&gt;PartInitializer allows you to override its default container through the CompositionHost.InitializeContainer method. This means you can create a container that is shared by both PartInitializer and the CompositionStarter class.&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; App_Startup(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender, StartupEventArgs e)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;{
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    var catalog = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; AggregateCatalog(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; AssemblyCatalog(Assembly.GetCallingAssembly()));&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    var container = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; CompositionContainer(catalog);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    CompositionHost.InitializeContainer(container);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    var starter = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; CompositionStarter&amp;lt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&amp;gt;();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    starter.Start();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;h1&gt;Using MEF within XAML in WPF&lt;/h1&gt;

&lt;p&gt;In the previous section I spoke about the PartInitializer API which we baked into Silverlight for composition in XAML, it lets you write code such as the following:&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; partial &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Dashboard : UserControl
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;{
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; MainPage()
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        InitializeComponent();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        PartInitializer.SatisfyImports(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (var widget &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; Widgets)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            TopWidgets.Items.Add(widget);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    [ImportMany]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; UserControl[] Widgets { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Above Dashboard is a user control that is created within XAML. It imports a collection of widgets. It forces itself to be composed by calling PartInitializer.SatisfyImports(). &lt;/p&gt;

&lt;p&gt;This is great for Silverlight, but what about WPF? Although we don’t yet have it in the box, I’ve ported our PartInitializer implementation to WPF. The functionality is almost identical though instead of looking in the main application XAP, it looks in the bin folder of the application and an optional extensions folder. You can get it on my skydrive &lt;a href="http://cid-f8b2fd72406fb218.skydrive.live.com/self.aspx/blog/Composition.Initialization.Desktop.zip"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Alternatively you can have the control which hosts Dashboard compose it by calling ComposeParts on the container and pass it in. Another option is to pass a container through the visual tree as an attached property and then have a helper method / another attached property which allows Dashboard to get composed without depending on a static container. More on this in another post.&lt;/p&gt;

&lt;h1&gt;Hosting MEF within a library&lt;/h1&gt;

&lt;p&gt;We’ve discussed an approach for hosting within an application, but what about a library? Should you use CompositionStarter then? I would err against that as it’s not ideal. For example take the case of a rules engine that uses MEF to discover rules. The rules engine itself needs the rules, so delegating off to IStartables to get the rules adds extra complexity with little gain. Having the rules engine host a container which it uses to get the rules is not at all a problem. Here is one way to implement it.&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; RulesEngine
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;{
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;readonly&lt;/span&gt; CompositionContainer _container;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; RulesEngineImports _rulesEngineImports;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; RulesEngine()
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (_container == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            _container = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; CompositionContainer(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; AssemblyCatalog(Assembly.GetCallingAssembly()));
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        var imports = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; RulesEngineImports();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        _container.ComposeParts(imports);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; RulesEngine(CompositionContainer container)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        _container = container;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; RulesEngine(&lt;span style="color: #0000ff"&gt;params&lt;/span&gt; ComposablePartCatalog[] catalogs)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        var aggregate = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; AggregateCatalog(catalogs);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        _container = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; CompositionContainer(aggregate);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; RulesEngineImports
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        [ImportMany]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IRule[] Rules { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}
&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;RulesEngine’s default ctor creates a default container though it accepts overrides for passing in a container and catalogs, this allows containers to be shared across other libraries that might use MEF. Once RulesEngine is created, the default ctor then creates a new RulesEngineImports class which it then composes. This class is basically a buddy class to get the imports that RulesEngine needs. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; I could have put the imports directly on RulesEngine, but in Silverlight these have to be public members as private reflection is not supported. I don’t want to expose the engines internal implementation to the outside and I want the engine to be reused in SL and WPF. Using this approach allows that reuse by keeping the imports as an internal implementation detail.&lt;/p&gt;

&lt;h2&gt;Why not use CommonServiceLocator?&lt;/h2&gt;

&lt;p&gt;I know there are going to be a bunch of folks reading this saying why should you couple the RulesEngine to MEF? Can’t you just use &lt;a href="http://commonservicelocator.codeplex.com/"&gt;CommonServiceLocator&lt;/a&gt; to get the rules the engine needs? Yes you absolutely could do that, though it will limit the capabilities you can leverage from a specific container. For example MEF currently supports metadata views on imports, something that is currently specific to MEF, though Autofac is catching up quickly :-) Additionally using CSL often results in icky service locatorish code all over the place, something we (the founders of CSL) never intended but which has happened.&lt;/p&gt;

&lt;p&gt;An alternative to using CSL that supports any container is to have an app-specific IRulesEngineServiceAdapter export which returns all the dependencies / imports that the engine needs. The service adapter can use whichever IoC it chooses, and it can leverage the full capabilities of that container. The engine could still use MEF to discover the adapter and then use the adapter from there on. &lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; RulesEngine
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;{
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;readonly&lt;/span&gt; CompositionContainer _container;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; IRulesEngineServiceAdapter _adapter;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; RulesEngine()
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (_container == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            _container = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; CompositionContainer(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; AssemblyCatalog(Assembly.GetCallingAssembly()));
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        _adapter = _container.GetExportedValue&amp;lt;IRulesEngineServiceAdapter&amp;gt;();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; RulesEngine(CompositionContainer container)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        _container = container;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; RulesEngine(&lt;span style="color: #0000ff"&gt;params&lt;/span&gt; ComposablePartCatalog[] catalogs)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        var aggregate = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; AggregateCatalog(catalogs);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        _container = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; CompositionContainer(aggregate);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;In the MEF case, the adapter would simply be a part which imports the rules. For other containers the implementation would differ.&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;[InheritedExport]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; IRulesEngineServiceAdapter
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;{
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;   IRule[] Rules { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; MefRulesEngineServiceAdapter : IRulesEngineServiceAdapter
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;{
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    [ImportMany]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IRule[] Rules { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;;}
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}
&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;/p&gt;

&lt;h1&gt;Hosting MEF within a web application or a web service&lt;/h1&gt;

&lt;p&gt;Using MEF within a multi-request server application such as a web app or a WCF service is a whole other ball of wax. You can use CompositionStarter in limited scenarios as long as you are dealing with singletons shared across all requests.&amp;#160; For more sophisticated scenarios you will need to create scoped per-request containers. That is outside of the scope of this post, but we will likely have future posts on this topic. &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h1&gt;Recap&lt;/h1&gt;

&lt;p&gt;We’ve just dug in to what it means to host MEF within an application / library, and various approaches for doing so. My goal for this post was simply to illustrate a range of options, use them or leave them depending on whether or not they work for your scenario. Don’t try to force a square peg into a round hole!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9956233" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category></item><item><title>Exporting dynamically changing configuration values</title><link>http://blogs.msdn.com/b/gblock/archive/2010/01/31/exporting-dynamically-changing-configuration-values.aspx</link><pubDate>Sun, 31 Jan 2010 21:46:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9956070</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=9956070</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2010/01/31/exporting-dynamically-changing-configuration-values.aspx#comments</comments><description>&lt;p&gt;In my last &lt;a href="http://codebetter.com/blogs/glenn.block/archive/2010/01/27/how-do-i-expose-configuration-information-through-mef.aspx"&gt;post&lt;/a&gt; I discussed how to export configuration. A follow up question in the comments was “What if the values change?” Meaning the values that are exported using the approach I illustrated are static. In the real world however values often can change based on some user setting etc. A variant of the approach however allows the values to not be static. Instead of exporting a value, export a Func which returns the value. MEF offers nice support for this through method exports. For example to allow the “recently used file” config item to change over time, we can modify the Exporter to export that value as a method rather than a property.&lt;/p&gt;  &lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; RecentlyUsedTrackerConfiguration
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;{
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; IConfigurationProvider _provider;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  [ImportingConstructor]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; RecentlyUsedTrackerConfiguration(IConfigurationProvider provider)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;     _provider = provider;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;     MaxItems = provider.GetValue&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt;(&amp;quot;&lt;span style="color: #8b0000"&gt;recentlyUsedTracker.maxItems&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  [Export(&amp;quot;&lt;span style="color: #8b0000"&gt;RecentlyUsedTracker.File&lt;/span&gt;&amp;quot;)]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; GetFile() {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; _provider.GetValue&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt;(&amp;quot;&lt;span style="color: #8b0000"&gt;recentlyUsedTracker.maxItems&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  [Export(&amp;quot;&lt;span style="color: #8b0000"&gt;RecentlyUsedTracker.MaxItems&lt;/span&gt;&amp;quot;)]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; MaxItems {&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;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Instead of exporting a property, we are now exporting&amp;#160; the GetFile method. Method exports are imported as delegates, thus RecentlyUsedTracker now looks like this.&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;[Export]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; RecentlyUsedTracker {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  [Import(&amp;quot;&lt;span style="color: #8b0000"&gt;RecentlyUsedTracker.File&lt;/span&gt;&amp;quot;)]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Func&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt; File {&lt;span style="color: #0000ff"&gt;get&lt;/span&gt;;&lt;span style="color: #0000ff"&gt;set&lt;/span&gt;;}
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  [Import(&amp;quot;&lt;span style="color: #8b0000"&gt;RecnetlyUsedTracker.MaxItems&lt;/span&gt;&amp;quot;)]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; MaxItems {&lt;span style="color: #0000ff"&gt;get&lt;/span&gt;;&lt;span style="color: #0000ff"&gt;set&lt;/span&gt;;}
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Notice above we are importing a Func&amp;lt;string&amp;gt; for the File item rather than string. This gives us the hook to dynamically change File each time it is accessed. You could even go further an parameterized File if you wanted to pass in context to determine which File is returned.&lt;/p&gt;

&lt;h1&gt;It’s still easy to test&lt;/h1&gt;

&lt;p&gt;With this approach RecentlyUsedTracker is still easy to test. With lambda syntax it is pretty painless to pass in test values when the class is under test.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9956070" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category></item><item><title>How do I expose configuration information through MEF?</title><link>http://blogs.msdn.com/b/gblock/archive/2010/01/27/how-do-i-expose-configuration-information-through-mef.aspx</link><pubDate>Wed, 27 Jan 2010 19:00:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9954237</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=9954237</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2010/01/27/how-do-i-expose-configuration-information-through-mef.aspx#comments</comments><description>&lt;p&gt;&amp;lt;Goal&amp;gt;Post in 15 mins or less :-)&amp;lt;/Goal&amp;gt;&lt;/p&gt;  &lt;p&gt;This question which recently popped on our &lt;a href="http://mef.codeplex.com/Thread/View.aspx?ThreadId=82114"&gt;forums&lt;/a&gt; is one of the common questions we hear from customers. In this particular case, wcoenen (the person in the forums) had recently used information that he wanted to pull from configuration and pipe to a part. He solved the problem by creating a special configuration provider service which he injected into the part to provide the config. The code for the consumer looked like this:&lt;/p&gt;  &lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;[ImportingConstructor]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; RecentlyUsedTracker(IConfigurationProvider configurationProvider)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;{   
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.recentlyUsedFile = configurationFile.GetValue&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt;(&amp;quot;&lt;span style="color: #8b0000"&gt;recentlyUsedTracker.File&lt;/span&gt;&amp;quot;);   
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.maxItems = configurationProvider.GetValue&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt;(&amp;quot;&lt;span style="color: #8b0000"&gt;recentlyUsedTracker.maxItems&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;As you can see above RecentlyUsedTracker takes an import of configuration provider. The solution above does work. The biggest issue wcoenen raised is that coupling of the part to the config provider makes testing more difficult. It requires a mock service to be passed in to every test. Another subtlety is that you won’t know if the data is actually present until the code executes.&lt;/p&gt;

&lt;h1&gt;An alternative approach, use property exports.&lt;/h1&gt;

&lt;p&gt;A different way to approach this problem is to actually export the individual configuration values. We can still use the generic configuration provider, but we can keep a nice separation of concerns.&amp;#160; The part can then import these values like any other import. MEF provides a very powerful facility for doing this called property exports. A property export is a property which is decorated with an export attribute. It is special because it provides a place for you to execute custom logic for retrieving a value and returning it to MEF, i.e. a custom factory. This is a different model than the traditional class-level exports which MEF is responsible for the creation . So using a property export we can easily surface configuration information and we can use any method we choose to get the value including the provider. For example, take a look at the code below.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; RecentlyUsedTrackerConfiguration
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;{
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; IConfigurationProvider _provider;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  [ImportingConstructor]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; RecentlyUsedTrackerConfiguration(IConfigurationProvider provider)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;     _provider = provider;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;     File = _provider.GetValue&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt;(&amp;quot;&lt;span style="color: #8b0000"&gt;recentlyUsedTracker.file&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;     MaxItems = provider.GetValue&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt;(&amp;quot;&lt;span style="color: #8b0000"&gt;recentlyUsedTracker.maxItems&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  [Export(&amp;quot;&lt;span style="color: #8b0000"&gt;RecentlyUsedTracker.File&lt;/span&gt;&amp;quot;)]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; File {&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;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  [Export(&amp;quot;&lt;span style="color: #8b0000"&gt;RecentlyUsedTracker.MaxItems&lt;/span&gt;&amp;quot;)]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; MaxItems {&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;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;RecentlyUsedTrackerConfiguration IS a part as you can see it has an importing constructor which takes a provider. But instead of exporting itself, it exports it’s properties which use the provider to get the values. It then assigns a string contract to each export which correlates to the configuration item it is exporting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: You can&amp;#160; make this more compile safe, by using constants or creating custom export attributes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now let’s take a look at our consumer.&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;[Export]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; RecentlyUsedTracker {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  [Import(&amp;quot;&lt;span style="color: #8b0000"&gt;RecentlyUsedTracker.File&lt;/span&gt;&amp;quot;)]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; File {&lt;span style="color: #0000ff"&gt;get&lt;/span&gt;;&lt;span style="color: #0000ff"&gt;set&lt;/span&gt;;}
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt; 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  [Import(&amp;quot;&lt;span style="color: #8b0000"&gt;RecnetlyUsedTracker.MaxItems&lt;/span&gt;&amp;quot;)]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; MaxItems {&lt;span style="color: #0000ff"&gt;get&lt;/span&gt;;&lt;span style="color: #0000ff"&gt;set&lt;/span&gt;;}
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}
&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;RecentlyUsedTracker imports File and MaxItems. It’s nice and clean (aside from the attributes :-) ). We can now easily test RecentlyUsedTracker, as he has no dependency on a provider. All he cares about is that the exports he needs are in the container.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h1&gt;An additional benefit the above approach introduces.&lt;/h1&gt;

&lt;p&gt;If you read my my or &lt;a href="blogs.msdn.com/nblumhardt"&gt;Nick’s&lt;/a&gt; blog’s, then you’ve heard us talk about Stable Composition and Rejection. Parts that don’t have there required dependencies are rejected. That means that RecentlyUsedTracker will be rejected unless the config items he needs are present. It’s consistent with the way the rest of MEF works it ensures consistency of the system. Of course if you &lt;em&gt;want&lt;/em&gt; to allow the part to be created even though the data is not present, you can always set the AllowDefault property on Import = true.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h1&gt;What else can use this approach for?&lt;/h1&gt;

&lt;p&gt;Using property exports has a million and one uses. Any time you need to integrate legacy or non-MEF components to MEF, property exports are usually a sure win! For example, let’s say you have an existing third-party class which you use in your system which is sealed. You can create that class in a property export and surface it to MEF. It’s also a great way to surface singletons that you don’t want parts to depend on. What do I mean? Check the code below as an example.&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; ApplicationPart
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;{
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  [Export]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Application App {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;{ &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; Application.Current;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;  }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Any questions?&lt;/strong&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9954237" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/gblock/archive/tags/WPF/">WPF</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/silverlight/">silverlight</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/-NET/">.NET</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category></item><item><title>MEF and Prism exploration, MEF module loading</title><link>http://blogs.msdn.com/b/gblock/archive/2010/01/03/mef-and-prism-exploration-mef-module-loading.aspx</link><pubDate>Sun, 03 Jan 2010 18:05:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9943151</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=9943151</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2010/01/03/mef-and-prism-exploration-mef-module-loading.aspx#comments</comments><description>&lt;p&gt;About a month ago I posted some ideas around &lt;a href="http://codebetter.com/blogs/glenn.block/archive/2009/12/02/mef-and-prism-to-be-or-not-to-be.aspx"&gt;MEF and Prism&lt;/a&gt; together. As part of the post, I presented 2 high-level scenarios for MEF/Prism integration (and there are likely more)&lt;/p&gt;  &lt;li&gt;Use Prism with MEF as an add-on for extensibility. That is make modules extensible through MEF.&amp;#160; You can think of this as the “Two worlds in harmony” solution. Your core architecture rests on Prism's modularity / and an IoC Container and sits side by side with MEF. &lt;/li&gt;  &lt;li&gt;Use MEF primarily for modularity, and pull in Prism bits for UI composition.&amp;#160; In this model, your core architecutre sits on top of MEF, with MEF replacing the services of your IoC.    &lt;p&gt;Recently &lt;a href="http://csharperimage.jeremylikness.com"&gt;Jeremy Likeness&lt;/a&gt; of Wintillect started exploring the first approach. He’s started a series of blog posts on his explorations down that path. If you check the series you’ll see how he is introducing a new type of module called a PartModule that then uses MEF. The approach he takes is nice because it uses all of the existing Prism module loading infrastructure including Unity, and then adds on that when the module is received, it is registered through MEF. He also leverages MEF’s recomposition capabilities such that when new things are received, they automatically show up as recomposable parts are recomposed.&lt;/p&gt;    &lt;p&gt;&lt;a href="http://gallery.jeremylikness.com/main.php?g2_itemId=154"&gt;&lt;img border="0" src="http://gallery.jeremylikness.com/main.php?g2_view=core.DownloadItem&amp;amp;g2_itemId=155&amp;amp;g2_serialNumber=2" /&gt;&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;Jeremy has 3 posts in the series so far, the content is nice and it includes all the code.&lt;/p&gt; &lt;/li&gt;  &lt;li&gt;&lt;a href="http://csharperimage.jeremylikness.com/2009/12/prism-mef-and-mvvm-part-1-of-3-unity.html"&gt;PRISM, MEF, and MVVM Part 1 of 3: Unity Glue&lt;/a&gt; &lt;/li&gt;  &lt;li&gt;&lt;a href="http://csharperimage.jeremylikness.com/2009/12/prism-mef-and-mvvm-part-2-of-3-making.html"&gt;PRISM, MEF, and MVVM Part 2 of 3: Making PRISM MEF Friendly&lt;/a&gt; &lt;/li&gt;  &lt;li&gt;&lt;a href="http://csharperimage.jeremylikness.com/#"&gt;PRISM, MEF, and MVVM Part 3 of 3: Dynamic MEF Modules in PRISM&lt;/a&gt;     &lt;p&gt;This is just one of what I am sure will be many explorations to come!&lt;/p&gt;    &lt;p&gt;&lt;/p&gt; &lt;/li&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9943151" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/gblock/archive/tags/silverlight/">silverlight</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/prism/">prism</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category></item><item><title>“MEF in 2 hours or less”, I am all for it.</title><link>http://blogs.msdn.com/b/gblock/archive/2009/12/31/mef-in-2-hours-or-less-i-am-all-for-it.aspx</link><pubDate>Thu, 31 Dec 2009 00:42:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9942457</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=9942457</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2009/12/31/mef-in-2-hours-or-less-i-am-all-for-it.aspx#comments</comments><description>&lt;P&gt;Some posts titles just really grab you. I couldn’t help but think about one of those annoying ads in the back of magazine when I saw this &lt;A href="http://ayende.com/Blog/archive/2009/12/18/how-to-write-mef-in-2-hours-or-less.aspx" mce_href="http://ayende.com/Blog/archive/2009/12/18/how-to-write-mef-in-2-hours-or-less.aspx"&gt;post&lt;/A&gt; from Ayende. In actuality the article is more of a “How I can implement what I need from MEF in 2 hours” however that’s not as provocative :-)&lt;/P&gt;
&lt;P&gt;The main point Ayende makes in the article is that folks talk about specific features in MEF like dynamic discovery and lazy loading as big deals, and he can implement something similar in a few hours. As I mentioned in the comments, I think Ayende is over generalizing on exactly what capabilities MEF actually provides, but I have no problem with that statement, just as I had no issue with Jeremy saying &lt;A href="http://codebetter.com/blogs/jeremy.miller/archive/2007/05/21/build-your-own-cab-part-1-the-preamble.aspx" mce_href="http://codebetter.com/blogs/jeremy.miller/archive/2007/05/21/build-your-own-cab-part-1-the-preamble.aspx"&gt;“Build your own CAB”&lt;/A&gt;. I went on to say (paraphrasing) “MEF is not built for you, it’s for the ecosystem”. &lt;/P&gt;
&lt;P&gt;What I mean by that is this, MEF is designed for supporting a rich ecosystem of extenders, it establishes a standard for extensibility that crosses application boundaries. If you are a third-party extending an application built on MEF, you just need to speak the language. You can walk up to any application that speaks that language and you are golden. Eclipse / OSGI is a prime example of this (not talking about the Experience but the extensibility). Visual Studio’s Editor in 2010 is another prime example. &lt;/P&gt;
&lt;P&gt;With MEF we’re saying you don’t have to be Eclipse / Visual Studio to provide the same type of experience. With MEF you can get off the ground with a few keystrokes, and without reading a 200 page manual. Of course that just gets you in the door, and as you scale up to use more advanced features MEF get’s more complex in fact, if you get to the Primitives you almost need to be a rocket scientist. However, you have a dial. If all you want to start with is using MEF for loading a small collection of plugins, you can do that with ease. Over time you can scale up according to your needs. As you scale you may&amp;nbsp;find many of those features that you may not need today are&amp;nbsp;a welcome addition.&lt;/P&gt;
&lt;P&gt;Now back to Ayende’s comment, if you are not using MEF for it’s ecosystem enabling capabilities, and you do not find the features it provides adequate, then feel free to NOT use it. Don’t worry, we’re not going to send the MEF police to your door! (Though don’t be surprised if I ping you personally over IM :-) )&lt;/P&gt;
&lt;P&gt;I am not saying that to drive you away from MEF, I am saying that saying NO is a valid option if it doesn’t meet your needs, and you can find/create something that does. If it does however meet your needs, saying no “just because” is silly. &lt;/P&gt;
&lt;P&gt;I’ll close with this statement, there’s much more to MEF than meets the eye. I advise taking it for a ride before you discard it. &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9942457" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category></item><item><title>Building Hello MEF – Part III – XAP Partitioning (with the host’s permission) and the sweetness of recomposition.</title><link>http://blogs.msdn.com/b/gblock/archive/2009/12/15/building-hello-mef-part-iii-xap-partitioning-with-the-host-s-permission-and-the-sweetness-of-recomposition.aspx</link><pubDate>Tue, 15 Dec 2009 21:31:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9937286</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=9937286</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2009/12/15/building-hello-mef-part-iii-xap-partitioning-with-the-host-s-permission-and-the-sweetness-of-recomposition.aspx#comments</comments><description>&lt;p&gt;In our last &lt;a href="http://blogs.msdn.com/gblock/archive/2009/12/05/building-hello-mef-part-ii-metadata-and-why-being-lazy-is-a-good-thing.aspx" mce_href="http://blogs.msdn.com/gblock/archive/2009/12/05/building-hello-mef-part-ii-metadata-and-why-being-lazy-is-a-good-thing.aspx"&gt;post&lt;/a&gt; we saw how using metadata in MEF allows us to provide additional self-describing information about exports. This has a range of uses including providing hints on how the export should be handled (such as UI location) as well as allowing us to filter out creating exports that we don’t need or which are not relevant to the current application context. In this post, we’ll see how we can now take our parts which all currently reside in a single XAP and we can split them up into multiple XAPs. We can use this for several reasons including reducing the general download time of our application, as well as allowing third-parties to extend our Silverlight applications. We’ll also see how we can combine dynamic download with a winning feature in MEF called recomposition.&lt;/p&gt;  &lt;p&gt;Just to remind you, this is what our dashboard looked like when we left off.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_4.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_4.png"&gt;&lt;img title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_thumb_1.png" width="653" height="375" mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_thumb_1.png" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;You can get the code for where we left out &lt;a href="http://cid-f8b2fd72406fb218.skydrive.live.com/self.aspx/blog/Hello%20MEF/HelloMEF%5E_Part%5E_II.zip" mce_href="http://cid-f8b2fd72406fb218.skydrive.live.com/self.aspx/blog/Hello%20MEF/HelloMEF%5E_Part%5E_II.zip"&gt;here&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;For our next exercise, we’re going to do something contrived though this is “based on a real story”, don’t worry. We’re going to take our dashboard app and extend it so that when we hit the “Hello MEF” button, new parts show up that are dynamically downloaded from a separate XAP on the server. I’ll show you two different ways of doing this, in this post we’ll do it with the host’s permission and in the next post without. &lt;/p&gt;  &lt;p&gt;Below is a diagram of what this looks like.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_20.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_20.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_thumb_7.png" width="566" height="151" mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_thumb_7.png" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;h2&gt;Where is the dynamic XAP download support in MEF? I don’t see it.&lt;/h2&gt;  &lt;p&gt;If you are familiar with MEF’s apis in Silverlight 4, you might be asking this question. The question is a valid one. The reason you don’t see it, is because it’s not there ;-) Instead we have initially shipped this feature as part of the Silverlight 4 Beta Toolkit, which you can download &lt;a href="http://www.codeplex.com/Silverlight/Release/ProjectReleases.aspx" mce_href="http://www.codeplex.com/Silverlight/Release/ProjectReleases.aspx"&gt;here&lt;/a&gt;. You’ll need this to follow along on this post.&lt;/p&gt;  &lt;h2&gt;Introducing the Package apis&lt;/h2&gt;  &lt;p&gt;If you download the toolkit, you’ll find our dynamic XAP support in the System.ComponentModel.Composition.Packaging.Toolkit.dll. The apis are below.&lt;/p&gt;  &lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Package
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Package(Uri packageUri, IEnumerable&amp;lt;Assembly&amp;gt; assemblies);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IEnumerable&amp;lt;Assembly&amp;gt; Assemblies { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; Package Current { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Uri Uri { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; DownloadPackageAsync(Uri packageUri, Action&amp;lt;AsyncCompletedEventArgs, Package&amp;gt; packageDownloadCompleted);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    }&lt;/pre&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; PackageCatalog : ComposablePartCatalog, INotifyComposablePartCatalogChanged
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; PackageCatalog();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IEnumerable&amp;lt;Package&amp;gt; Packages { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; IQueryable&amp;lt;ComposablePartDefinition&amp;gt; Parts { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;event&lt;/span&gt; EventHandler&amp;lt;ComposablePartCatalogChangeEventArgs&amp;gt; Changed;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;event&lt;/span&gt; EventHandler&amp;lt;ComposablePartCatalogChangeEventArgs&amp;gt; Changing;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; AddPackage(Package package);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;The api has two parts. First there is the Package class. Package has a static method called DownloadPackageAsync which you pass a Uri for a XAP as well as a completed action. As soon as you call this method, MEF will begin the download. Once the XAP is received, it will invoke the callback passing in an instance of Package. In the callback handler, the next step is to add the Package to a PackageCatalog. In order for MEF to see the PackageCatalog it must have been added to an instance of MEF’s container. Until now we’ve avoided talking about the container but well learn a bit about it shortly. Once the new package is added, it will then be available to parts in the container. We’ll see how with recomposition, parts that import any of the exports in the new package will suddenly “light up” with the newly available parts.&lt;/p&gt;

&lt;p&gt;OK, let’s get going.&lt;/p&gt;

&lt;h2&gt;Creating a separate XAP.&lt;/h2&gt;

&lt;p&gt;The first thing we need to do to get going is create a separate XAP. You might be surprised if you go looking for the “Create Secondary XAP” template, as there is none :-) However, all hope is not lost. Fortunately we can create a XAP by using the Silverlight Application template. Right click on the HelloMEF solution can click&amp;#160; “Add New Project”. Enter HelloMEF.Extensions as the project name.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_8.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_8.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_thumb_3.png" width="751" height="521" mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_thumb_3.png" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Once you do, hit OK on the next page and you’ll get a new Silverlight application. Next go into the project and remove the App.Xaml and &lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image19.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image19.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image19_thumb.png" width="485" height="182" mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image19_thumb.png" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;One last step, we need to set our web application so it copies our new XAP to the output folder. Fortunately the tooling support for SL4 brings this to VS2010 out of the box. If you right click on the HelloMEF.Web project and check the “Silverlight Applications” tab, you’ll see.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_12.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_12.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_thumb_5.png" width="732" height="495" mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_thumb_5.png" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;I am guessing this was not intended for the application partitioning solution and was rather for having multiple Silverlight applications in a single web application. However, it works great for our scenario so we’re not complaining! &lt;/p&gt;

&lt;p&gt;Once you create the new application, VS automatically creates a new start page in your web site which it sets as the default. We need to reset it back, so go right-click on HelloMEFTestPage.html under HelloMEF.Web and set it as the startup page.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_18.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_18.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_thumb_6.png" width="442" height="508" mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_thumb_6.png" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;Create a contract assembly&lt;/h2&gt;

&lt;p&gt;Now that we’ve created our separate XAP, we can now go start building our new extension. OK, all we do is go and create our widget, but wait, how do we get access to the Widget contract? Currently all the widget definition information resides in the app. We could just go and a reference back to the application right? Technically you could as the app won’t be referencing us directly, but please DON’T do it. Every time you do a puppy dies :-)&lt;/p&gt;

&lt;p&gt;What we need to is to split our contracts out into a separate assembly so that those definitions can be shared across XAPs. We don’t have to embed the shared library over and over fortunately.&lt;/p&gt;

&lt;p&gt;First thing we’ll do is create a new Silverlight Class library project. You know the drill, right click the solution, new project…&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_2.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_thumb.png" width="735" height="509" mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_thumb.png" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Delete the Class1.cs file. Then go cut/paste ExportWidgetAttribute.cs, WidgetLocation.cs and IWidgetMetadata.cs from the HelloMEF project into HelloMEF.Contracts. You will then need to add a reference to System.ComponentModel.Composition (which should be in your recent references) in the new project. Remember it’s in the “.\Program Files\Microsoft SDKs\Silverlight\v4.0\Libraries\Client folder. &lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_15.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_15.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_thumb_2.png" width="655" height="524" mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_thumb_2.png" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Next go add back to the HelloMEF project and add a reference to the new HelloMEF.Contracts assembly.&lt;/p&gt;

&lt;p&gt;Compile the project. If you followed each of the above step correctly it “should” compile. If it doesn’t work, you can grab a working version &lt;a href="http://cid-f8b2fd72406fb218.skydrive.live.com/self.aspx/blog/Hello%20MEF/HelloMEF%5E_Part%20IIIa.zip" mce_href="http://cid-f8b2fd72406fb218.skydrive.live.com/self.aspx/blog/Hello%20MEF/HelloMEF%5E_Part%20IIIa.zip"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Building a dynamically downloadable widget&lt;/h2&gt;

&lt;p&gt;First we need to add a reference to MEF in our HelloMEF.Extensions project as we did above. Next we’ll add a reference to HelloMEF.Contracts. After adding it, go right-click on the reference and set Copy-Local to false in the reference properties. As our shared library is referenced by the app we know it will be loaded into memory and we shouldn’t copy the assembly over and over. If we do, we could run into trouble.&lt;/p&gt;

&lt;p&gt;Now go add a new UserControl to the HelloMEF.Extensions project. Call it Widget3. We’ll make this have a textbox with a green border. Below is the Widget3.xaml.&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;UserControl&lt;/span&gt; &lt;span style="color: #ff0000"&gt;x&lt;/span&gt;:&lt;span style="color: #ff0000"&gt;Class&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;HelloMEF.Extensions.Widget3&amp;quot;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &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;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #ff0000"&gt;xmlns&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;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #ff0000"&gt;xmlns&lt;/span&gt;:&lt;span style="color: #ff0000"&gt;d&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;http://schemas.microsoft.com/expression/blend/2008&amp;quot;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #ff0000"&gt;xmlns&lt;/span&gt;:&lt;span style="color: #ff0000"&gt;mc&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;http://schemas.openxmlformats.org/markup-compatibility/2006&amp;quot;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #ff0000"&gt;mc&lt;/span&gt;:&lt;span style="color: #ff0000"&gt;Ignorable&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;d&amp;quot;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #ff0000"&gt;d&lt;/span&gt;:&lt;span style="color: #ff0000"&gt;DesignHeight&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;300&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;d&lt;/span&gt;:&lt;span style="color: #ff0000"&gt;DesignWidth&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;400&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;       &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Grid&lt;/span&gt; &lt;span style="color: #ff0000"&gt;x&lt;/span&gt;:&lt;span style="color: #ff0000"&gt;Name&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;LayoutRoot&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Height&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;Auto&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;TextBox&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Background&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;Green&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Text&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;Hello MEF!&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;TextAlignment&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;Center&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Height&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;100&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Width&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;300&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;       &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Grid&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;UserControl&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p mce_keep="true"&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Next go drop in the code behind and make it a widget.&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;namespace&lt;/span&gt; HelloMEF.Extensions
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;{
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    [ExportWidget(Location=WidgetLocation.Bottom)]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; partial &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Widget3 : UserControl
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Widget3()
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            InitializeComponent();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}
&lt;/pre&gt;&lt;/pre&gt;

&lt;h2&gt;Initializing the container to support dynamic download&lt;/h2&gt;

&lt;p&gt;If you are new to MEF, you might not be aware that behind the scenes the PartInitializer uses a class called the CompositionContainer to do all the work. By default we create a container which has all the exports in the current XAP. You can however override that container to do special things, like support dynamic XAP download. You override the container by calling the CompositionHost.InitializeContainer method. &lt;/p&gt;

&lt;p&gt;Add the following code to the App.xaml.cs class in the main app.&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; InitializeContainer()
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;{
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    var catalog = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; PackageCatalog();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    catalog.AddPackage(Package.Current);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    var container = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; CompositionContainer(catalog);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    container.ComposeExportedValue(catalog);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    CompositionHost.InitializeContainer(container);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}
&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;The code above is performing several functions.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;It creates a PackageCatalog. Catalogs are a generic concept and they provide parts to the container. In this case the PackageCatalog will provide parts found in XAPs. Future downloaded XAPs will get added to this catalog. &lt;/li&gt;

  &lt;li&gt;It adds Package.Current, which contains all the assemblies in the main XAP. &lt;/li&gt;

  &lt;li&gt;Creates a container which uses the new catalog. &lt;/li&gt;

  &lt;li&gt;Composes the catalog into the container. This makes the PackageCatalog available to importers so they can use it. You can also wrap the PackageCatalog in a service like IPackageService if you like and export it that way, but I am keeping it simple. &lt;/li&gt;

  &lt;li&gt;Finally it overrides PartInitializer’s container with this new container. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we have one more thing to do which is to call our new method. Replace the Application_Startup method with the following code.&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&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, StartupEventArgs e)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;{
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    InitializeContainer();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.RootVisual = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; MainPage();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}
&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Now compile and run the app to test that the overridden container is working. You should the same result.&lt;/p&gt;

&lt;h2&gt;Enabling a widget to download new widgets&lt;/h2&gt;

&lt;p&gt;We’ve added our widget, but it’s not showing up, why? Well, because we haven’t used the Package class to download it. Let’s do that now. First thing we need to do is add a reference to the packaging dll. You’ll need to look in your Toolkit folder.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_22.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_22.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_thumb_8.png" width="486" height="388" mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_thumb_8.png" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Next we’ll change Widget1 to go and download our new widgets. We’ll start with adding a using statement for System.ComponentModel.Composition.Packaging at the top, and then we’ll implement the logic.&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.ComponentModel.Composition;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Windows.Controls;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Windows;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.ComponentModel.Composition.Packaging;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;namespace&lt;/span&gt; HelloMEF
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;{
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    [ExportWidget(Location=WidgetLocation.Top)]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; partial &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Widget1 : UserControl
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        [Import]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; PackageCatalog Packages { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Widget1()
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            InitializeComponent();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            Button.Click += &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; RoutedEventHandler(Button_Click);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Button_Click(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            Package.DownloadPackageAsync(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Uri(&amp;quot;&lt;span style="color: #8b0000"&gt;HelloMEF.Extensions.xap&lt;/span&gt;&amp;quot;,UriKind.Relative), (args, p) =&amp;gt; Packages.AddPackage(p));
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}
&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Here’s what we’ve done.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;We added an import to get to our PackageCatalog that was explicitly added to the container. &lt;/li&gt;

  &lt;li&gt;We’ve subscribed to the button click event. &lt;/li&gt;

  &lt;li&gt;In the handler, we’ve used the static Package.DownloadPackageAsync method to grab “HelloMEF.Extensions.xap”. The method takes a delegate, which we are passing a lambda to for adding the package upon it’s receipt. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now compile and run the app. Once the app is running, press the button and…….&lt;strong&gt;Exception&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_24.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_24.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_thumb_9.png" width="621" height="379" mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_thumb_9.png" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;What’s happening is our new package has been downloaded, but it has been rejected by MEF, why? Oh, due to non-recomposable import, of course :-) What in the world is that? :-) Let’s scroll and look at the rest of the exception.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_26.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_26.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_thumb_10.png" width="468" height="91" mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_thumb_10.png" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;What MEF is basically saying here is that new exports have showed up in our XAP which will satisfy the import MainPage.Widgets, but they have been rejected because the Widgets import itself is not recomposable.&lt;/p&gt;

&lt;h2&gt;Rejection and Recomposition&lt;/h2&gt;

&lt;p&gt;We just saw two things coming into play here. First MEF rejects things sometimes. In this case it is rejecting the new parts in the catalog due to recomposition, but it will also reject for other reasons such as a part having an import which cannot be satisfied because no exports are present. Rejecting is part of a very powerful feature called Stable Composition feature which we built MEF on top of and which your systems can rely on. For more on Stable Composition, check this &lt;a href="http://blogs.msdn.com/gblock/archive/2009/08/02/stable-composition-in-mef-preview-6.aspx" mce_href="http://blogs.msdn.com/gblock/archive/2009/08/02/stable-composition-in-mef-preview-6.aspx"&gt;post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;But what is Recomposition? Recomposition means that parts in MEF are not static once they are composed and they can opt in to be recomposed as new things show up, recomposed means all recomposable imports are re-satisfied. If you connect the dots, that means your systems on top of MEF are dynamic. There’s one caveat though, you have to opt-in to recomposition, otherwise parts are rejected. &lt;/p&gt;

&lt;p&gt;Doing that is as simple as setting AllowComposition = True in the Import / ImportMany attribute as we’ve done below.&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Windows.Controls;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.ComponentModel.Composition;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;namespace&lt;/span&gt; HelloMEF
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;{
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; partial &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; MainPage : UserControl
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; MainPage()
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            InitializeComponent();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            PartInitializer.SatisfyImports(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (var widget &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; Widgets)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;                &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (widget.Metadata.Location == WidgetLocation.Top)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;                    TopWidgets.Items.Add(widget.Value);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;                &lt;span style="color: #0000ff"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (widget.Metadata.Location == WidgetLocation.Bottom)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;                    BottomWidgets.Items.Add(widget.Value);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        [ImportMany(AllowRecomposition=&lt;span style="color: #0000ff"&gt;true&lt;/span&gt;)]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Lazy&amp;lt;UserControl,IWidgetMetadata&amp;gt;[] Widgets { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Now run the app, press the button and we don’t get an exception.&lt;/p&gt;

&lt;p mce_keep="true"&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_4.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_4.png"&gt;&lt;img title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_thumb_1.png" width="653" height="375" mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_thumb_1.png" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Awesome, but aren’t we back where we started? There’s no new widget, where is it?&lt;/p&gt;

&lt;h2&gt;Designing for Recomposition&lt;/h2&gt;

&lt;p&gt;The widgets are there, they are just not showing. Why? Because we populated the widgets when the control loaded. The Widgets property has been updated, but the MainPage doesn’t know to do anything when it is. What we need to do is make some changes to our MainPage to make it recomposition aware. There are two ways we could do this. First we could change our auto property to a real property, and override the getter when it is set. This is a bit ugly, so let’s not do that. &lt;/p&gt;

&lt;p&gt;Instead let’s take advantage of an interface in MEF which although it has a hideous name, is very nice. It’s called IPartImportsSatisfactionNotification :-) What it does is allow the part to get notified by MEF whenever recomposition occurs. Thus, the perfect place for our logic for populating the widgets, AND we can still use automatic properties.&lt;/p&gt;

&lt;p&gt;Below is the code for MainPage to do this.&lt;/p&gt;

&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Windows.Controls;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.ComponentModel.Composition;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;span style="color: #0000ff"&gt;namespace&lt;/span&gt; HelloMEF
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;{
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; partial &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; MainPage : UserControl, IPartImportsSatisfiedNotification
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; MainPage()
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            InitializeComponent();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            PartInitializer.SatisfyImports(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        [ImportMany(AllowRecomposition=&lt;span style="color: #0000ff"&gt;true&lt;/span&gt;)]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Lazy&amp;lt;UserControl,IWidgetMetadata&amp;gt;[] Widgets { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        #region IPartImportsSatisfiedNotification Members
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; OnImportsSatisfied()
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            TopWidgets.Items.Clear();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            BottomWidgets.Items.Clear();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (var widget &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; Widgets)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;                &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (widget.Metadata.Location == WidgetLocation.Top)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;                    TopWidgets.Items.Add(widget.Value);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;                &lt;span style="color: #0000ff"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (widget.Metadata.Location == WidgetLocation.Bottom)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;                    BottomWidgets.Items.Add(widget.Value);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;            }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;        #endregion
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 10px"&gt;}
&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;You can see now that when composition occurs, we are clearing our two controls and repopulating. In future post we’ll see how we can use a ViewModel to make this code much cleaner.&lt;/p&gt;

&lt;p&gt;OK, drum roll please. Compile and Run the app, press the button and….&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_28.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_28.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_thumb_11.png" width="837" height="498" mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIIXAPPartitioninga_1543/image_thumb_11.png" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Voila, dynamic download of XAPs!&lt;/p&gt;

&lt;h2&gt;A few caveats about our Package API&lt;/h2&gt;

&lt;p&gt;I am sure by now you are thinking, wow this looks great! However, there are some unsupported capabilities when using our Package API that you should be aware of.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Does not support Silverlight caching. Meaning if your XAP has references to things that are not in the XAP, the will only be found if they have been previously loaded, as the contracts are because the main app references them. &lt;/li&gt;

  &lt;li&gt;Does not support loose resources such as images, xaml files, etc sitting in the XAP. They must be resources embedded in assemblies. &lt;/li&gt;

  &lt;li&gt;Does not support versioning. Let’s say you have a XAP that uses version 1 of a type, and another XAP that uses version 2. The loader will load both rather than unify on the latest version. &lt;/li&gt;

  &lt;li&gt;Does not support localization, though you can probably come up with your own localization mechanism. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;In this post, we learned how we can add dynamic XAP downloading support to our applications. &lt;/p&gt;

&lt;p&gt;Along the way we also learned the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;MEF has a CompositionContainer behind the scenes which it uses for composition. This container can be overridden. &lt;/li&gt;

  &lt;li&gt;MEF Contracts can be shared across XAPs and do not need to be embedded in every XAP. &lt;/li&gt;

  &lt;li&gt;MEF rejects parts that it cannot work with. &lt;/li&gt;

  &lt;li&gt;MEF supports recomposition allowing parts to be recomposed when new things show up. &lt;/li&gt;

  &lt;li&gt;IPartImportsSatisfiedNotification allows us to get notifications within our parts when they are recomposed. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This opens up a Pandora’s box of power. It can be used for good or evil :-) On the good side we can use it to allow our Silverlight apps to be extended by third-parties, as well as to partition our apps for a better user experience.&lt;/p&gt;

&lt;h2&gt;What’s next&lt;/h2&gt;

&lt;p&gt;In the next post we’ll explore this scenario a bit further. We’ll see how you can create a part that dynamically downloads XAPs without needing PartInitializer to be configured to allow it. MEF on!&lt;/p&gt;

&lt;p&gt;Code is attached.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9937286" width="1" height="1"&gt;</description><enclosure url="http://cid-f8b2fd72406fb218.skydrive.live.com/self.aspx/blog/Hello%20MEF/HelloMEF%5E_Part%5E_III%20Completed.zip" length="53446" type="application/xhtml+xml; charset=utf-8" /><category domain="http://blogs.msdn.com/b/gblock/archive/tags/silverlight/">silverlight</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/Silverlight+4/">Silverlight 4</category></item><item><title>Building Hello MEF – Part II – Metadata and why being Lazy is a good thing.</title><link>http://blogs.msdn.com/b/gblock/archive/2009/12/05/building-hello-mef-part-ii-metadata-and-why-being-lazy-is-a-good-thing.aspx</link><pubDate>Sat, 05 Dec 2009 01:47:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9932866</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>13</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=9932866</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2009/12/05/building-hello-mef-part-ii-metadata-and-why-being-lazy-is-a-good-thing.aspx#comments</comments><description>&lt;P&gt;In Part I of the series we built the basics of our dashboard end ended with our app displaying a single widget. In this post we’ll show two widgets,&amp;nbsp; sensing a pattern here?&amp;nbsp; :-) We will show two, but we’ll put them in different places on our dashboard. We’ll explore two ways of doing this including a very powerful feature in MEF called metadata. Then we’ll see how we can use custom exports to provide our own “language’ of sorts for our extenders.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;If you are just jumping into this post, you can grab the code for where we left off &lt;A href="http://cid-f8b2fd72406fb218.skydrive.live.com/self.aspx/blog/Hello%20MEF/HelloMEF%5E_Part%5E_I.zip" mce_href="http://cid-f8b2fd72406fb218.skydrive.live.com/self.aspx/blog/Hello%20MEF/HelloMEF%5E_Part%5E_I.zip"&gt;here&lt;/A&gt;.&lt;/P&gt;
&lt;H1&gt;Adding a second widget&lt;/H1&gt;
&lt;P&gt;OK, here we go. Now that we have our dashboard infrastructure in place, should be easy to add a second widget right? Right. Let’s do it, this time we’ll add a text box.&lt;/P&gt;
&lt;P&gt;First we’ll go create a new user control called Widget2&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_2.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_2.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_thumb.png" width=517 height=380 mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Next we’ll go into the Widget2.xaml and add a button with the content “Hello MEF”.&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;UserControl&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;x&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;Class&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"HelloMEF.Widget2"&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #ff0000"&gt;xmlns&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"http://schemas.microsoft.com/winfx/2006/xaml/presentation"&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #ff0000"&gt;xmlns&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;x&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"http://schemas.microsoft.com/winfx/2006/xaml"&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #ff0000"&gt;xmlns&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;d&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"http://schemas.microsoft.com/expression/blend/2008"&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #ff0000"&gt;xmlns&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;mc&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"http://schemas.openxmlformats.org/markup-compatibility/2006"&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #ff0000"&gt;mc&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;Ignorable&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"d"&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #ff0000"&gt;d&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;DesignHeight&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"300"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;d&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;DesignWidth&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"400"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Grid&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;x&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;Name&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"LayoutRoot"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;Height&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"Auto"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;TextBox&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;Text&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"Hello MEF!"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;TextAlignment&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"Center"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;Height&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"100"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;Width&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"300"&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Grid&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;UserControl&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;Then we’ll jump into Widget2.xaml.cs and add our export.&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.ComponentModel.Composition;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.Windows.Controls;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.Windows;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;namespace&lt;/SPAN&gt; HelloMEF
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;{
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    [Export(&lt;SPAN style="COLOR: #0000ff"&gt;typeof&lt;/SPAN&gt;(UserControl))]
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; partial &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; Widget1 : UserControl
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; Widget1()
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            InitializeComponent();
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;}&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;All we did was create a new control and drop an export (the only MEF port). Let’s run it.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_6.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_6.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_thumb_2.png" width=653 height=387 mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_thumb_2.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Presto! &lt;/STRONG&gt;The second widget shows up. No app.config editing, no explicit registration code. Just add it to the project and we’re done. Not bad!&lt;/P&gt;
&lt;H1&gt;Adding a second widget where we want it, and why being Lazy is a good thing.&lt;/H1&gt;
&lt;P&gt;OK well that’s pretty nice. But if you read my first post, we want to have widgets showing up in different places as we have 2 panels for widgets in our screen. One which is blue, and one which is yellow but currently invisible. So what can we do? Well one option is to create different interfaces, ITopWidget, IBottomWidget. Then we can check if the class implements those interfaces once we get it and just put it in the right place.&lt;/P&gt;
&lt;P&gt;If we did, our code would in our MainPage would look something like this&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; MainPage()
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;{
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    InitializeComponent();
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    PartInitializer.SatisfyImports(&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;foreach&lt;/SPAN&gt; (var widget &lt;SPAN style="COLOR: #0000ff"&gt;in&lt;/SPAN&gt; Widgets)
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (widget &lt;SPAN style="COLOR: #0000ff"&gt;is&lt;/SPAN&gt; ITopWidget)
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            TopWidgets.Items.Add(widget);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;else&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (widget &lt;SPAN style="COLOR: #0000ff"&gt;is&lt;/SPAN&gt; IBottomWidget)
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            BottomWidgets.Items.Add(widget);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;}
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;That works, however it forces us to ship new interfaces every time I we want to enhance the UI and add a left pane, we need to ship ILeftWidget. A different problem is that we are creating all widgets even if they don’t implement any of our interfaces. But what else can we do?&lt;/P&gt;
&lt;H2&gt;Lazy&lt;/H2&gt;
&lt;P&gt;The answer is to become Lazy. Yes folks, this is the one time when Lazy is a good thing. If you are not familar, &lt;A href="http://msdn.microsoft.com/en-us/library/dd642331(VS.96).aspx" mce_href="http://msdn.microsoft.com/en-us/library/dd642331(VS.96).aspx"&gt;Lazy&amp;lt;T&amp;gt;&lt;/A&gt; is new type in the SL4 and FX4 BCL that allows you to delay the creation of an instance. MEF fully supports Lazy, thus you can import single values and collections in a delayed manner. For example we can change our ImportMany to use Lazy, by pasting the following code in MainPage.&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.Windows.Controls;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.ComponentModel.Composition;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;namespace&lt;/SPAN&gt; HelloMEF
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;{
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; partial &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; MainPage : UserControl
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; MainPage()
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            InitializeComponent();
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            PartInitializer.SatisfyImports(&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            &lt;SPAN style="COLOR: #0000ff"&gt;foreach&lt;/SPAN&gt; (var widget &lt;SPAN style="COLOR: #0000ff"&gt;in&lt;/SPAN&gt; Widgets)
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;                TopWidgets.Items.Add(widget.Value);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        [ImportMany]
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; Lazy&amp;lt;UserControl&amp;gt;[] Widgets { &lt;SPAN style="COLOR: #0000ff"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: #0000ff"&gt;set&lt;/SPAN&gt;; }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;}
&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;Two things you should notice above, first our Widgets import is now an array of Lazy&amp;lt;UserControl&amp;gt;, and second in the loop we are adding widget.Value rather than the widget value directly. What is going on here is instead of importing UserControl instances we are importing lazy references to our widgets. The first time Value is accessed, the UserControl is created, thus we access .Value to add the widgest to the TopWidgets ItemsControl.&lt;/P&gt;
&lt;H2&gt;Metadata&lt;/H2&gt;
&lt;P&gt;Well that’s nice, because now we’ve delayed creating the controls until we actually need them. The downside is we have no way to determine which one’s we should put where which was our goal. And THAT is where metadata comes to the rescue. Not only does MEF support lazy instantiation, but it also supports a concept called export metadata. This means that a MEF export can give off additional information about itself that the importer can use for several reasons, whether it be determining whether or not it should be created, or in our case determining where it will show on the screen. The importer can access this information BEFORE the instance has actually been activate. This means if the particular export does not match the requirements, then it does not have to be created.&lt;/P&gt;
&lt;P&gt;Metadata is arbitrary in MEF, that means you can use it however you see fit according to your application needs. Using metadata has two parts, first there is the exporter who defines which metadata is available for importers to look at. Then there is the importer who is able to access the metadata at the time of import. &lt;/P&gt;
&lt;H2&gt;Defining the metadata for widgets&lt;/H2&gt;
&lt;P&gt;In our case we want our widgets to provide us a location. In order to do that, we’ll first create a new enum called WidgetLocation.&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;namespace&lt;/SPAN&gt; HelloMEF
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;{
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;enum&lt;/SPAN&gt; WidgetLocation {Top, Bottom}
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;}
&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;Then we’ll go add a piece of metadata called Location to our widgets. There are several ways to do this in MEF, the easiest of which to start with is simple adding an ExportMetadata attribute. First we’ll go to Widget1.xaml.cs and set the widget’s location to the top as you can see below. Notice, the key and value are arbitary to my specific needs. &lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    [ExportMetadata("&lt;SPAN style="COLOR: #8b0000"&gt;Location&lt;/SPAN&gt;", WidgetLocation.Top)]
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    [Export(&lt;SPAN style="COLOR: #0000ff"&gt;typeof&lt;/SPAN&gt;(UserControl))]
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; partial &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; Widget1 : UserControl&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; Widget1()
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            InitializeComponent();
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    }&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;Next we’ll go to Widget2 and tell it to set it’s location to the bottom.&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    [ExportMetadata("&lt;SPAN style="COLOR: #8b0000"&gt;Location&lt;/SPAN&gt;", WidgetLocation.Bottom)]
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    [Export(&lt;SPAN style="COLOR: #0000ff"&gt;typeof&lt;/SPAN&gt;(UserControl))]
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; partial &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; Widget2 : UserControl&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; Widget2()
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            InitializeComponent();
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;P&gt;    }&lt;/P&gt;&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;Now we can move on to the importer&lt;/P&gt;
&lt;H2&gt;Giving the dashboard access to the widget metadata and why Lazy has a brother.&lt;/H2&gt;
&lt;P&gt;Now that we’ve defined the metadata we just need to use it. We talked about Lazy&amp;lt;T&amp;gt;, let’s talk about it’s brother &lt;A href="http://msdn.microsoft.com/en-us/library/dd986615(VS.96).aspx" mce_href="http://msdn.microsoft.com/en-us/library/dd986615(VS.96).aspx"&gt;Lazy&amp;lt;T,M&amp;gt;&lt;/A&gt;. In order to allow us to access the metadata, MEF introduces a special kind of Lazy, one that has attached metadata. M in this case is an interface (we call it a metadata view)&amp;nbsp; that contains only getter properties where the property name corresponds to the key of a piece of metadata. MEF will automatically general a proxy class that implements this interface and it will plug all the metadata in for you. This is very cool! You need to see it to believe it (and understand it).&lt;/P&gt;
&lt;P&gt;So let’s just do it rather than talking about it.&lt;/P&gt;
&lt;P&gt;First, we’ll create a new interface call IWidgetMetadata which contains our Location metadata.&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;namespace&lt;/SPAN&gt; HelloMEF
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;{
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;interface&lt;/SPAN&gt; IWidgetMetadata
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; WidgetLocation Location {&lt;SPAN style="COLOR: #0000ff"&gt;get&lt;/SPAN&gt;;}
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;}&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;Next we’ll go into our MainPage.xaml.cs, and change our Widgets import to use the new metadata. We’ll also change the logic to place the widgets in the appropriate place. &lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.Windows.Controls;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.ComponentModel.Composition;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;namespace&lt;/SPAN&gt; HelloMEF
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;{
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; partial &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; MainPage : UserControl
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; MainPage()
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            InitializeComponent();
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            PartInitializer.SatisfyImports(&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            &lt;SPAN style="COLOR: #0000ff"&gt;foreach&lt;/SPAN&gt; (var widget &lt;SPAN style="COLOR: #0000ff"&gt;in&lt;/SPAN&gt; Widgets)
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;                &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (widget.Metadata.Location == WidgetLocation.Top)
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;                    TopWidgets.Items.Add(widget.Value);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;                &lt;SPAN style="COLOR: #0000ff"&gt;else&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (widget.Metadata.Location == WidgetLocation.Bottom)
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;                    BottomWidgets.Items.Add(widget.Value);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        [ImportMany]
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; Lazy&amp;lt;UserControl,IWidgetMetadata&amp;gt;[] Widgets { &lt;SPAN style="COLOR: #0000ff"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: #0000ff"&gt;set&lt;/SPAN&gt;; }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;}
&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;Above you can see that Widget’s item type is now Lazy&amp;lt;UserControl,IWidgetMetadata&amp;gt;. Also in the for loop we’re accessing widget.Metadata.Location in a compile safe manner, but we never created a concrete class that implements IWidgetMetadata, did we? We didn’t, but MEF did :-) The result is no magic strings on the importer side, we get intellisense, refactoring and compile time checking! Also notice we still need to access the .Value to get the widget to be created. This is actually an advantage though because if we get a widget with a different location, it won’t get created wastefully. Here it makes no big difference, but if we had 50 widgets that is a different story.&lt;/P&gt;
&lt;P&gt;Now we’ll go run the app.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_4.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_4.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_thumb_1.png" width=653 height=375 mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_thumb_1.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;And Voila we see our widgets are now going in the right place, and we didn’t have to do 1000 back flips.&lt;/P&gt;
&lt;H3&gt;Magic strings are bad! And MEF has an answer.&lt;/H3&gt;
&lt;P&gt;Notice above I mentioned we get all this compile time safety and intellisense support for the importer. But what about the exporter? Our widgets are still specifying magic strings for the keys. Not only that, but imagine if I have multiple pieces of metadata, then it’s not just one string, it’s many strings for each part. Plus I lose a lot of discoverability because I don’t know intuitively when I create a widget, what metadata keys make sense. Even if we create constants for the keys, we don’t know the type.&lt;/P&gt;
&lt;P&gt;Wouldn’t it be really nice if we could just do this:&amp;nbsp; [ExportWidget(Location=WidgetLocation.Top)] ? We wouldn’t need Export, and we wouldn’t need all those loose ExportMetadata attributes. It would be cleaner, and our code would be less noisy.&lt;/P&gt;
&lt;P&gt;The good news is you can. &lt;/P&gt;
&lt;H1&gt;Custom exports&lt;/H1&gt;
&lt;P&gt;MEF let’s you create custom exports that include metadata. A custom export is just that, it’s your own export attribute with metadata, and it’s strongly typed.&lt;/P&gt;
&lt;P&gt;Let’s create our new ExportWidgetAttribute.&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.Windows.Controls;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.ComponentModel.Composition;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;namespace&lt;/SPAN&gt; HelloMEF
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;{
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    [MetadataAttribute]
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    [AttributeUsage(AttributeTargets.Class, AllowMultiple=&lt;SPAN style="COLOR: #0000ff"&gt;false&lt;/SPAN&gt;)]
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; ExportWidgetAttribute : ExportAttribute
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; ExportWidgetAttribute()
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            :&lt;SPAN style="COLOR: #0000ff"&gt;base&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #0000ff"&gt;typeof&lt;/SPAN&gt;(UserControl))
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; WidgetLocation Location { &lt;SPAN style="COLOR: #0000ff"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: #0000ff"&gt;set&lt;/SPAN&gt;; }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;}&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;If we look above there are a few things to note:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;ExportWidgetAttribute inherits from ExportAttribute. This is what tells MEF “Hey I am an export attribute” &lt;/LI&gt;
&lt;LI&gt;The base constructor on Export is called passing in UserControl. This step is critical or MEF will default to the concrete implementation that the attribute was dropped on. &lt;/LI&gt;
&lt;LI&gt;The attribute has a [MetadataAttribute] on it, this tells MEF this is not only an export but it also provides metadata. Now that this is applied, MEF will look at each public property on the attribute that I added, and will assume it is metadata, with the name being the key. In essence it will add an [ExportMetadata(“Location”,…)] with whatever value is specified for the property. &lt;/LI&gt;
&lt;LI&gt;The attribute has an [AttributeUsage] attribute specifying the acceptable targets i.e. class in this case and that AllowMultiple = false. If we don’t do this, then MEF will assume multiple sets of metadata can exist and will return the metadata as an array. &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Now we can go and change each of our widgets to use the new attribute. &lt;/P&gt;
&lt;P&gt;First Widget1.xaml.cs&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.Windows.Controls;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;namespace&lt;/SPAN&gt; HelloMEF
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;{
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    [ExportWidget(Location=WidgetLocation.Top)]
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; partial &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; Widget1 : UserControl
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; Widget1()
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            InitializeComponent();
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    }
&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;And then Widget2.xaml.cs&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.Windows.Controls;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;namespace&lt;/SPAN&gt; HelloMEF
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;{
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    [ExportWidget(Location=WidgetLocation.Bottom)]
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; partial &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; Widget2 : UserControl
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; Widget2()
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            InitializeComponent();
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;}&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;Our custom attributes our quite nice. No more specifying a contract, no more loose&amp;nbsp; metadata, and it’s nice and compact. Also notice our usings above as well, no more need to specify System.ComponentModel.Composition…..it’s our attribute. This means our customers can get going using MEF without even knowing they are using it. We just send them OUR attributes and OUR interfaces, and they are good to go.&lt;/P&gt;
&lt;P&gt;Go and press the magic run button and this what you get.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_4.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_4.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_thumb_1.png" width=653 height=375 mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingHelloMEFPartIIMetadataCustomExp_2681/image_thumb_1.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;H2&gt;Any questions? :-)&lt;/H2&gt;
&lt;H1&gt;&amp;nbsp;&lt;/H1&gt;
&lt;H1&gt;In Summary&lt;/H1&gt;
&lt;P&gt;In this post we learned how we can use metadata and Lazy to do some pretty nice things with MEF. We can annotate our exports with information that importers can use to determine whether they want to use us, and how they should us (i.e. put me in the top location). We also learned how can create our own custom expors to offer a nice experience.&lt;/P&gt;
&lt;H1&gt;What’s next?&lt;/H1&gt;
&lt;P&gt;In the next post we’ll explore how to partition our apps into multiple XAPs and use MEF to deliver them on-demand. We’ll also learn a few other aspects of MEF that may not be obvious. There’s much more to come after that, the series is shaping up!&lt;/P&gt;
&lt;P&gt;Code is attached&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9932866" width="1" height="1"&gt;</description><enclosure url="http://cid-f8b2fd72406fb218.skydrive.live.com/self.aspx/blog/Hello%20MEF/HelloMEF%5E_Part%5E_II.zip" length="50444" type="application/xhtml+xml; charset=utf-8" /><category domain="http://blogs.msdn.com/b/gblock/archive/tags/silverlight/">silverlight</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/Silverlight+4/">Silverlight 4</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/SL4/">SL4</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/SL/">SL</category></item><item><title>MEF and Prism, to be or not to be.</title><link>http://blogs.msdn.com/b/gblock/archive/2009/12/02/mef-and-prism-to-be-or-not-to-be.aspx</link><pubDate>Wed, 02 Dec 2009 23:58:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9931693</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>10</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=9931693</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2009/12/02/mef-and-prism-to-be-or-not-to-be.aspx#comments</comments><description>&lt;P&gt;Now to the question, what is the story on Prism and MEF? I am getting asked this now several times a day, more so than the IoC question. &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;I am happy to say we’re working with p&amp;amp;p to come up with a good answer. &lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Note: This is still a work-in-progress, but here’s&amp;nbsp;the thinking on this so far.&lt;/P&gt;
&lt;P&gt;There is some obvious overlap but also a clear delineation. In particular when it comes to UI composition. MEF can be used for UI composition for sure, however it doesn’t have specific UI composition bits like RegionManager, Composite Commands, EventAggregator. Something like EventAgg will come in the future, but it is highly unlikely we will ever deliver some of the Prism UI-specific bits as part of MEF. That could change, but at least today that is the case.&lt;/P&gt;
&lt;P&gt;In general though here are the differences.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;First, MEF is in the platform as part of SL4/FX4. &lt;/LI&gt;
&lt;LI&gt;Second, MEF is a general UI agnostic composition technology while Prism is specific for UI composition. MEF also extends beyond composite applications to general extensibility / plugin models. &lt;/LI&gt;
&lt;LI&gt;Third, Prism and MEF can work together. &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;There are two primary use cases&amp;nbsp;we hear from customers who want to use MEF and Prism together.&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Use Prism with MEF as an add-on for extensibility. That is make modules extensible through MEF.&amp;nbsp; You can think of this as the&amp;nbsp;“Two worlds in harmony” solution. Your core architecture rests on Prism's modularity / and an&amp;nbsp;IoC Container&amp;nbsp;and sits side by side with&amp;nbsp;MEF.&lt;/LI&gt;
&lt;LI&gt;Use MEF primarily for modularity, and pull in Prism bits for UI composition.&amp;nbsp; In this model, your core architecutre sits on top of MEF, with MEF replacing the services of your IoC. &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Note: You can also use MEF for some aspects of UI composition, particularly for pull-style regions which import their UI elements.&lt;/P&gt;
&lt;P&gt;The first angle is primarily for customers who have existing applications that use Prism and want to use MEF. The second is for customers who are building new apps in SL4/FX4, that want to use MEF for providing the foundation, and pull in CAL libraries where appropriate.&amp;nbsp; I think both make equal sense depending on customer needs.&lt;/P&gt;
&lt;P&gt;In terms of which to use when, my advice is it depends on your situation. &lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;If you are already committed to Prism, stick with it. There will be guidance coming for integrating MEF and Prism in Prism 4. &lt;/LI&gt;
&lt;LI&gt;If you are starting fresh (and you are choosing to use MEF) then you may to use MEF for modularity, and general aspects of your composite app and pull in Prism pieces where appropriate.&lt;/LI&gt;&lt;/UL&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9931693" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/gblock/archive/tags/prism/">prism</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category></item><item><title>MEF Refresh of Preview 8 for Silverlight</title><link>http://blogs.msdn.com/b/gblock/archive/2009/12/02/mef-refresh-of-preview-8-for-silverlight.aspx</link><pubDate>Wed, 02 Dec 2009 23:35:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9931685</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=9931685</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2009/12/02/mef-refresh-of-preview-8-for-silverlight.aspx#comments</comments><description>&lt;p&gt;&lt;strong&gt;Note: This only applies to using MEF Preview 8 on Silverlight, it does not apply to MEF in Silverlight 4&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Yes I know we never did a Preview 8 post but……&lt;/p&gt;  &lt;p&gt;We found a bug in PartInitializer (thanks to &lt;a href="http://msmvps.com/blogs/kathleen/archive/2009/12/02/mef-preview-8-bug-not-finding-exports-in-silverlight-when-using-partinitializer.aspx"&gt;Kathleen Dollard&lt;/a&gt; for finding it!)&amp;#160; in Preview 8 which affects MEF in Silverlight. The bug resulted in the default container that PI creates not being populated with any assemblies from the current XAP, which meant your imports would not get satisfied. This affected our Extensible Grid sample in that when you launched it you would not see any grid extensions.&lt;/p&gt;  &lt;p&gt;The bug has now been fixed and we refreshed the release on 11/11.&lt;/p&gt;  &lt;p&gt;Please &lt;a href="http://mef.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=34838#DownloadId=88937"&gt;download&lt;/a&gt; the latest bits to get the fix if you downloaded Preview 8 before the 11th.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9931685" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/gblock/archive/tags/silverlight/">silverlight</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/Silverlight+4/">Silverlight 4</category></item><item><title>Building the Hello MEF dashboard in Silverlight 4 - Part I</title><link>http://blogs.msdn.com/b/gblock/archive/2009/11/30/building-the-hello-mef-dashboard-in-silverlight-4-part-i.aspx</link><pubDate>Mon, 30 Nov 2009 10:28:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9930099</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=9930099</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2009/11/30/building-the-hello-mef-dashboard-in-silverlight-4-part-i.aspx#comments</comments><description>&lt;P&gt;In my last post I illustrated some of the basics of MEF through a Hello MEF dashboard app that I used in my PDC talk. In this series of posts, we’ll build that application from scratch and then go even further than we did in that talk. This might take several posts, I am not sure yet, we’ll just have to see ;-) We’ll build the app in an incremental fashion, revisiting different parts along the way in order to introduce new functionality. &lt;/P&gt;
&lt;P&gt;Before you invest the time, I’ll give you a preview of the topics we’ll cover along the way.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Exports / Imports / Part Initializer – All that jazz &lt;/LI&gt;
&lt;LI&gt;Export metadata &lt;/LI&gt;
&lt;LI&gt;Custom exports &lt;/LI&gt;
&lt;LI&gt;Overriding Part Initializer &lt;/LI&gt;
&lt;LI&gt;Dynamic XAP downloading &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;In this post, we’ll cover just the first bullet.&lt;/P&gt;
&lt;P&gt;To follow along, you will need Silverlight 4 and the Silverlight 4 toolkit. You can download Silverlight 4 beta&amp;nbsp; &lt;A href="http://www.silverlight.net/getstarted/silverlight-4-beta" mce_href="http://www.silverlight.net/getstarted/silverlight-4-beta"&gt;here&lt;/A&gt; and the toolkit &lt;A href="http://silverlight.codeplex.com/" mce_href="http://silverlight.codeplex.com/"&gt;here&lt;/A&gt;.&lt;/P&gt;
&lt;H2&gt;The App&lt;/H2&gt;
&lt;P&gt;Below is a screenshot of the dashboard application.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_2.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_2.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_thumb.png" width=536 height=356 mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;May not look very complicated, but what we’re showing here is actually quite sophisticated. The dashboard itself is a dumb shell. The two “Hello MEF” widgets you see are actually exports that are discovered by MEF. The dashboard itself has not knowledge of what it will find, but it does know how to place things that are provided to it, and it has different locations on the screen where things can go. If you’re familiar with Prism, these are very similar to regions in principle though instead of modules pushing content into regions, MEF is pulling the content from whatever is available. More on this later.&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Note: I’ll admit, I suck as a designer though I think I could be good if I REALLY try :-) Nice thing about SL is you don’t have to be, as your friendly neighborhood designer can make something hideous a work of art. With that note, don't expect a work of art out of this post.&lt;/EM&gt;&lt;/P&gt;
&lt;H2&gt;Step 1 – Create the app&lt;/H2&gt;
&lt;P&gt;Nothing fancy here, just create a new Silverlight application. Enter HelloMEF as the name.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_4.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_4.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_thumb_1.png" width=427 height=274 mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_thumb_1.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Leave the “Host the Silverlight application in a new Web Site” checked. We’ll use this later as we get into XAP partitioning.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_6.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_6.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_thumb_2.png" width=392 height=314 mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_thumb_2.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;H2&gt;Step 2. Create the dashboard UI.&lt;/H2&gt;
&lt;P&gt;For this dashboard we’re going to create something simple, amazingly simple, amazingly stupidly simple. But it works. We’ll create a StackPanel with two items control which will be populated with our widges. Jump into your MainPage.xaml and paste the following.&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;UserControl&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;x&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;Class&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"HelloMEF.MainPage"&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #ff0000"&gt;xmlns&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"http://schemas.microsoft.com/winfx/2006/xaml/presentation"&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #ff0000"&gt;xmlns&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;x&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"http://schemas.microsoft.com/winfx/2006/xaml"&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #ff0000"&gt;xmlns&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;d&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"http://schemas.microsoft.com/expression/blend/2008"&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #ff0000"&gt;xmlns&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;mc&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"http://schemas.openxmlformats.org/markup-compatibility/2006"&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #ff0000"&gt;mc&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;Ignorable&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"d"&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #ff0000"&gt;d&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;DesignHeight&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"300"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;d&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;DesignWidth&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"400"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;StackPanel&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;x&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;Name&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"LayoutRoot"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;Background&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"Black"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Border&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;Background&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"Cyan"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            &lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;ItemsControl&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;x&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;Name&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"TopWidgets"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;Height&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"Auto"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;FontSize&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"30"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;ItemsControl&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Border&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Border&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;Background&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"Yellow"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            &lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;ItemsControl&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;x&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;Name&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"BottomWidgets"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;Height&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"Auto"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;FontSize&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"30"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;ItemsControl&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Border&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;StackPanel&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;UserControl&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/PRE&gt;
&lt;H2&gt;Step 3. Find and show the widgets – Importing many&lt;/H2&gt;
&lt;P&gt;Our dashboard is supposed to populate itself with widgets. The first thing we need to think about is what exactly is a widget :-) In MEF when we have the concept of a contract. A c&lt;EM&gt;ontract&lt;/EM&gt; represents something that will either be provided (exported) or consumed (imported). The easiest way to think about it for now is what type is the extension. In the case of our dashboard let’s just assume each widget is a UserControl. We could create a new base type or an interface, but we don’t need to. UserControl will do just fine. &lt;/P&gt;
&lt;P&gt;So now we need to tell MEF we need a collection of UserControl, that is we want to import many UserControls. And for that we have the ImportMany attribute.&lt;/P&gt;
&lt;P&gt;First add a reference to System.ComponentModel.Composition, and System.ComponentModel.Composition.Initialization.dll. To find them you’ll need to browse to your Silverlight SDK directory (C:\Program Files\Microsoft SDKs\Silverlight\v4.0\Libraries\Client). Next go into the code-behind of MainPage.xaml.cs and overwrite the class with the following. &lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.Windows.Controls;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.ComponentModel.Composition;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;namespace&lt;/SPAN&gt; HelloMEF
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;{
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; partial &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; MainPage : UserControl
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; MainPage()
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            InitializeComponent();
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        [ImportMany]
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; UserControl[] Widgets { &lt;SPAN style="COLOR: #0000ff"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: #0000ff"&gt;set&lt;/SPAN&gt;; }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;}
&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;Basically we’re saying “MEF, give me the widgets”&lt;/P&gt;
&lt;P&gt;Once we have the widgets, we need to show them on the screen. For now, let’s assume all widgets will go in the same ItemsControl. Let’s add the logic to the constructor. &lt;BR&gt;&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.Windows.Controls;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.ComponentModel.Composition;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;namespace&lt;/SPAN&gt; HelloMEF
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;{
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; partial &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; MainPage : UserControl
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; MainPage()
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            InitializeComponent();
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            &lt;SPAN style="COLOR: #0000ff"&gt;foreach&lt;/SPAN&gt; (var widget &lt;SPAN style="COLOR: #0000ff"&gt;in&lt;/SPAN&gt; Widgets)
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;                TopWidgets.Items.Add(widget);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        [ImportMany]
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; UserControl[] Widgets { &lt;SPAN style="COLOR: #0000ff"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: #0000ff"&gt;set&lt;/SPAN&gt;; }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;}
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;/PRE&gt;&lt;/PRE&gt;
&lt;H2&gt;Step 4: Create a widget – Export&lt;/H2&gt;
&lt;P&gt;Now that we’ve got our basic Dashboard setup, let’s create a widget. Right click on the “HelloMEF” project and add a new Silverlight UserControl specifying Widget1 for the name.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_8.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_8.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_thumb_3.png" width=521 height=334 mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_thumb_3.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Our widget is also going to be dead simple, and simply be a button. Go into Widget1.xaml and paste the following.&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;UserControl&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;x&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;Class&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"HelloMEF.Widget1"&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #ff0000"&gt;xmlns&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"http://schemas.microsoft.com/winfx/2006/xaml/presentation"&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #ff0000"&gt;xmlns&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;x&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"http://schemas.microsoft.com/winfx/2006/xaml"&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #ff0000"&gt;xmlns&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;d&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"http://schemas.microsoft.com/expression/blend/2008"&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #ff0000"&gt;xmlns&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;mc&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"http://schemas.openxmlformats.org/markup-compatibility/2006"&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #ff0000"&gt;mc&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;Ignorable&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"d"&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #ff0000"&gt;d&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;DesignHeight&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"300"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;d&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;DesignWidth&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"400"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Grid&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;x&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;Name&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"LayoutRoot"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;Height&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"Auto"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Button&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;x&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff0000"&gt;Name&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"Button"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;Content&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"Hello MEF!"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;Width&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"300"&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;Height&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #0000ff"&gt;"100"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Button&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;Grid&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;UserControl&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;Now we need to tell MEF that we want to provide our widget to importers of UserControl. To do this we will Export it. Go into HelloMEF.Widget1.cs and paste the following:&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.ComponentModel.Composition;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.Windows.Controls;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.Windows;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;namespace&lt;/SPAN&gt; HelloMEF
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;{
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    [Export(&lt;SPAN style="COLOR: #0000ff"&gt;typeof&lt;/SPAN&gt;(UserControl))]
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; partial &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; Widget1 : UserControl
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; Widget1()
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            InitializeComponent();
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;}
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;Notice above we added an Export attribute and specified UserControl as the type. If we did not pass the type, then MEF would have just exported Widget1 as itself, which would not be of much use as the dashboard will never find it! That being said there are times when it does make sense to use the concrete type, but this is not one of them :-)&lt;/P&gt;
&lt;H2&gt;Step 5 – Compose the dashboard&lt;/H2&gt;
&lt;P&gt;OK, we’ve implemented our Dashboard, specified our imports, and created our widget. That means if we run the app, we’ll see widgets right? Wrong :-) Try running and you’ll see this nasty exception.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_14.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_14.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_thumb_6.png" width=486 height=313 mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_thumb_6.png"&gt;&lt;/A&gt;&amp;nbsp;&amp;nbsp; &lt;BR&gt;What happened? Our Widgets property is null, but why? The reason is because we never told MEF to do anything with it? Yes we put an ImportMany attribute, but MEF needs to somehow our MainPage in order to know it needs to provide anything. Which brings us to our last concept, &lt;EM&gt;composition&lt;/EM&gt;. &lt;/P&gt;
&lt;P&gt;In MEF there are basically 3 things you need to do. First two, declare your exports and imports which we did already. Third, tell MEF to actually go and compose, that is satisfy all imports. The places to do this (you can do it more than once) are where there are 1 or more imports and no exports. In this case our MainPage is the place where we import Widgets so that is the place. &lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Note: We won’t put it in the widgets themselves as they export themselves. Why not? Because when MEF pulls on an export, it automatically satisfies any of the imports on the thing it pulls. There is an exception to this, but we’ll cover it later.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;In general whenever you have user controls that are created in XAML, you’ll use this technique. Telling MEF to compose in Silverlight takes only about 12 lines of code. It’s really easy code and 12 lines that are well worth it. &lt;/P&gt;
&lt;P&gt;Kidding :-)&lt;/P&gt;
&lt;P&gt;To tell MEF to compose you invoke a single call, PartInitializer.SatisfyImports(this). In MEF, anything that has imports or exports we consider a Part, which is why the name. Below is the code added in MainPage.cs&lt;/P&gt;&lt;PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.Windows.Controls;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.ComponentModel.Composition;
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;namespace&lt;/SPAN&gt; HelloMEF
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;{
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; partial &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; MainPage : UserControl
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; MainPage()
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        {
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            InitializeComponent();
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            PartInitializer.SatisfyImports(&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;            &lt;SPAN style="COLOR: #0000ff"&gt;foreach&lt;/SPAN&gt; (var widget &lt;SPAN style="COLOR: #0000ff"&gt;in&lt;/SPAN&gt; Widgets)
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;                TopWidgets.Items.Add(widget);
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        [ImportMany]
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; UserControl[] Widgets { &lt;SPAN style="COLOR: #0000ff"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: #0000ff"&gt;set&lt;/SPAN&gt;; }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;    }
&lt;/PRE&gt;&lt;PRE style="BACKGROUND-COLOR: #ffffff; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 10px"&gt;}
&lt;/PRE&gt;&lt;/PRE&gt;
&lt;P&gt;Now when we Run, we see our first widget show up.&amp;nbsp; &lt;/P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_16.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_16.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_thumb_7.png" width=484 height=268 mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/BuildingtheHelloMEFdashboardinSIlverlig_C0/image_thumb_7.png"&gt;&lt;/A&gt; 
&lt;H2&gt;&amp;nbsp;&lt;/H2&gt;
&lt;H2&gt;Summary&lt;/H2&gt;
&lt;P&gt;OK, it’s 2:17 am, time to go to bed. I guess this will run several posts after all. :-)&lt;/P&gt;
&lt;P&gt;In this post we’ve seen the basics of exporting, importing and composition in MEF. We’ve also touched on the concept of contracts and parts, which we will delve further into the future.&lt;/P&gt;
&lt;P&gt;In the next post we’ll cover the following&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Providing / Accessing export metadata &lt;/LI&gt;
&lt;LI&gt;Other ways to import and a deeper look at contracts &lt;/LI&gt;
&lt;LI&gt;Providing custom exports &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;PS: This is not my normal style of posting. I am following in the lead of my mentor, guru and overall awesome guy &lt;A href="http://blogs.msdn.com/brada" mce_href="http://blogs.msdn.com/brada"&gt;Brad Abrams&lt;/A&gt;. Please feel free to give feedback on how I can improve. I am looking forward to taking this trip with you.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Code is attached.&lt;/STRONG&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9930099" width="1" height="1"&gt;</description><enclosure url="http://cid-f8b2fd72406fb218.skydrive.live.com/self.aspx/blog/Hello%20MEF/HelloMEF%5E_Part%5E_I.zip" length="48094" type="application/xhtml+xml; charset=utf-8" /><category domain="http://blogs.msdn.com/b/gblock/archive/tags/silverlight/">silverlight</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/Silverlight+4/">Silverlight 4</category></item><item><title>MEF has landed in Silverlight 4. We come in the name of extensibility.</title><link>http://blogs.msdn.com/b/gblock/archive/2009/11/29/mef-has-landed-in-silverlight-4-we-come-in-the-name-of-extensibility.aspx</link><pubDate>Sun, 29 Nov 2009 11:10:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9929815</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=9929815</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2009/11/29/mef-has-landed-in-silverlight-4-we-come-in-the-name-of-extensibility.aspx#comments</comments><description>&lt;P&gt;&lt;IMG title="" alt="UFO over highway B83 by Markusram." src="http://farm4.static.flickr.com/3607/3641457838_b068c63554.jpg" width=541 height=412 mce_src="http://farm4.static.flickr.com/3607/3641457838_b068c63554.jpg"&gt;&lt;/P&gt;
&lt;P&gt;&lt;A title=http://www.flickr.com/photos/markusram/3641457838/ href="http://www.flickr.com/photos/markusram/3641457838/" mce_href="http://www.flickr.com/photos/markusram/3641457838/"&gt;http://www.flickr.com/photos/markusram/3641457838/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;At PDC we announced that MEF has is now part of Silverlight 4. &lt;/P&gt;
&lt;P&gt;This may sound like alien speak :-), so I’ll break it down for you. It means building maintainable/pluggable RIA apps just got a whole lot easier.&amp;nbsp; &lt;/P&gt;
&lt;H1&gt;What is MEF?&lt;/H1&gt;
&lt;P&gt;The Managed Extensibility Framework (MEF) is a new library in Silverlight 4 for building RIAs that can be incrementally extended. With MEF you extend your apps in a declarative fashion and let MEF do the plumbing rather than writing all the plumbing code yourself.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;It’s for you and your dev team&lt;/STRONG&gt; – You can add and replace functionality to the system without having modify existing code. This means you and your QA team are happy because you don’t have to worry about introducing bugs into the existing code base. &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;It’s for your customers&lt;/STRONG&gt; – MEF allows you to design applications and libraries that can be extended by third-parties after they have been deployed.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Always there, always ready&lt;/STRONG&gt; – In Silverlight, MEF is ready to go whenever and wherever you need it. You don’t have to be an acrobat, or have a masters degree in complexity :-) You just do it.&lt;/LI&gt;&lt;/UL&gt;
&lt;H1&gt;Also available in Silverlight 3&lt;/H1&gt;
&lt;P&gt;MEF ships as part of Silverlight 4, but we’ve also made a Silverlight compatible version of MEF available on codeplex at MEF.Codeplex.com.&lt;/P&gt;
&lt;H1&gt;The Basics&lt;/H1&gt;
&lt;P&gt;&lt;STRONG&gt;THREE&lt;/STRONG&gt; concepts, that’s all you need to know to get going.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_4.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_4.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_thumb_1.png" width=454 height=361 mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_thumb_1.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Export to tell MEF you are providing a service or capability.&lt;/LI&gt;
&lt;LI&gt;Import to tell MEF you need something.&lt;/LI&gt;
&lt;LI&gt;Compose It – To tell MEF to go do it!&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Below you can see the basics in action for creating a Hello MEF pluggable dashboard. First we create a pluggable widget which in this case is a simple user control. &lt;/P&gt;
&lt;H2&gt;Export it&lt;/H2&gt;
&lt;P&gt;To tell MEF it’s available, we simply throw an Export attribute on it and specify the contract (in this case we use a type) you want to export it as. We could choose any type that the Widget implements, but to keep it simple, lets just use UserControl. As you can see, we don’t have to derive from any special base types, or use any interfaces, you use whatever class you would like.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_8.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_8.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_thumb_3.png" width=454 height=341 mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_thumb_3.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;H2&gt;Import It&amp;nbsp; - Import single&lt;/H2&gt;
&lt;P&gt;Notice our Widget&amp;nbsp; has a Message property. Let’s let MEF provide that rather than hardcoding it as I want it to be customizable. To tell MEF we need it, we’ll just slap on an [Import] attribute. If we don’t specify a contract for the import, it will just derive the contract as being a string. However, being that we don’t want just ANY string, we want the Message, we can specify a specific contract such as “HelloMEF.Message”.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_14.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_14.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_thumb_6.png" width=454 height=341 mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_thumb_6.png"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Next let’s create our message which our widget(s) will import. To do this we’ll create an export, but it will use a feature of MEF called a Field Export. This is because we don’t own System.String and can’t derive from it, instead we want to somehow create an instance of a string and export it as our message. With a Field Export we can do just that as can be seen below. If our needs are beyond simply setting a value which we can do in a field, MEF also supports property exports.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_22.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_22.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_thumb_10.png" width=454 height=342 mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_thumb_10.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;H2&gt;Import It – Import many&lt;/H2&gt;
&lt;P&gt;Lastly we need our host app to discover the extensions. To do that we’ll do two things. First, we&amp;nbsp;will create a public collection of type UserControl. Next we’ll add an ImportMany attribute, telling MEF that our MainPage needs all exports of UserControl to be injected into it. &lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_26.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_26.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_thumb_12.png" width=454 height=342 mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_thumb_12.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;H2&gt;Compose It – Satisfy imports&lt;/H2&gt;
&lt;P&gt;Next we need to somehow tell MEF to go do some work and give it to us. Doing this is very easy, we just drop into the constructor of our MainPage and call the SatisfyImports method. Once we do, our Widgets collection will be loaded up with all available widgets. &lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_18.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_18.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_thumb_8.png" width=454 height=341 mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_thumb_8.png"&gt;&lt;/A&gt;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Now we can simply loop through that collection and show the widgets, in this case by adding them to an Items control.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;A href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_28.png" mce_href="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_28.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_thumb_13.png" width=454 height=342 mce_src="http://blogs.msdn.com/blogfiles/gblock/WindowsLiveWriter/MEFhaslandedinSilverlight4.Wecomeinthen_1801/image_thumb_13.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;We’ve just seen the core essentials of MEF in SL4, that’s all it takes to get going! Our dashboard works, but it’s pretty simplistic. This is just the beginning of what is possible with MEF, we’ve literally only scratched the surface, however even with this small set of tools I’ve just given you, you can accomplish a lot! In future posts (on this blog or others :-) ) and in our Silverlight documentation you’ll see more about what’s possible. I covered some of this in my talk including lazy instantiation, export metadata, custom exports,&amp;nbsp;dynamic recomposition, and application partitioning.&lt;/P&gt;
&lt;H2&gt;PDC talk&lt;/H2&gt;
&lt;P&gt;At PDC, I had the pleasure of delivering a talk on building extensible RIAs which is now available online &lt;A href="http://microsoftpdc.com/Sessions/FT24" mce_href="http://microsoftpdc.com/Sessions/FT24"&gt;here&lt;/A&gt;. &lt;STRONG&gt;As a highlight I was joined by &lt;A href="http://blogs.microsoft.co.il/blogs/arielbh/" mce_href="http://blogs.microsoft.co.il/blogs/arielbh/"&gt;Ariel Ben-Horesh&lt;/A&gt; of &lt;A href="http://www.sela.co.il/?CategoryID=452" mce_href="http://www.sela.co.il/?CategoryID=452"&gt;Sela&lt;/A&gt;, and &lt;A href="http://jacksonito.blogspot.com/" mce_href="http://jacksonito.blogspot.com/"&gt;Jackson Harper&lt;/A&gt; of the &lt;A href="http://www.novell.com/products/desktop/moonlight.html" mce_href="http://www.novell.com/products/desktop/moonlight.html"&gt;Moonlight&lt;/A&gt; team at &lt;A href="http://www.novell.com/home/" mce_href="http://www.novell.com/home/"&gt;Novell&lt;/A&gt;.&amp;nbsp; &lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Check out my PDC talk: &lt;A href="http://microsoftpdc.com/Sessions/FT24" mce_href="http://microsoftpdc.com/Sessions/FT24"&gt;&lt;FONT color=#669966&gt;Building Extensible Rich Internet Applications with the Managed Extensibility Framework&lt;/FONT&gt;&lt;/A&gt;. In addition to seeing MEF in action, you’ll see some work around integrating MEF with Prism and even using MEF on Moonlight, the open source Silverlight implementation. More on this in a future post!&lt;/P&gt;
&lt;P&gt;You’ll also see how to surgically add extensibility to an existing application, and how to dynamically partition applications such that functionality is delivered on demand at runtime!&lt;/P&gt;
&lt;H2&gt;Can’t wait, want more?&lt;/H2&gt;
&lt;UL&gt;
&lt;LI&gt;Check out the attached demos (Yes code!) from that talk. (Requires Silverlight 4) &lt;/LI&gt;
&lt;LI&gt;Head over to MEF.Codeplex.com and download &lt;A href="http://mef.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=34838" mce_href="http://mef.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=34838"&gt;MEF Preview 8&lt;/A&gt; which contains several Silverlight samples including an extensible DataGrid. These samples work on SL3 or on SL4.&lt;/LI&gt;
&lt;LI&gt;Check out Brad Abrams &lt;A href="http://blogs.msdn.com/brada/archive/tags/MEF/Silverlight/default.aspx" mce_href="http://blogs.msdn.com/brada/archive/tags/MEF/Silverlight/default.aspx"&gt;series&lt;/A&gt; on MEF and Silverlight &lt;/LI&gt;
&lt;LI&gt;Check out Erik Mork’s &lt;A href="http://development-guides.silverbaylabs.org/Video/Silverlight-MEF" mce_href="http://development-guides.silverbaylabs.org/Video/Silverlight-MEF"&gt;screencast&lt;/A&gt; on MEF and Silverlight.&lt;/LI&gt;
&lt;LI&gt;Check out these &lt;A href="http://www.sparklingclient.com/index.php?s=MEF&amp;amp;searchbutton=Go%21" mce_href="http://www.sparklingclient.com/index.php?s=MEF&amp;amp;searchbutton=Go%21"&gt;podcasts&lt;/A&gt; on MEF and Silverlight&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Also watch for &lt;A href="http://blogs.silverlight.net/blogs/jesseliberty" mce_href="http://blogs.silverlight.net/blogs/jesseliberty"&gt;Jesse Liberty&lt;/A&gt; and &lt;A href="http://timheuer.com/blog/" mce_href="http://timheuer.com/blog/"&gt;Tim Heuer’s&lt;/A&gt; blogs as you’ll likely see some MEF/Silverlight content there VERY soon. ;-)&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9929815" width="1" height="1"&gt;</description><enclosure url="http://cid-f8b2fd72406fb218.skydrive.live.com/self.aspx/blog/PDC09%20Demos.zip" length="67564" type="application/xhtml+xml; charset=utf-8" /><category domain="http://blogs.msdn.com/b/gblock/archive/tags/silverlight/">silverlight</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category></item><item><title>Building extensible RIAs at PDC</title><link>http://blogs.msdn.com/b/gblock/archive/2009/11/16/building-extensible-rias-at-pdc.aspx</link><pubDate>Mon, 16 Nov 2009 18:14:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9923065</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=9923065</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2009/11/16/building-extensible-rias-at-pdc.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://microsoftpdc.com/"&gt;&lt;img title="image" border="0" alt="image" align="left" src="http://blogs.msdn.com/blogfiles/brada/WindowsLiveWriter/PDC2009Icantwait_88A8/image_3.png" width="325" height="88" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;This week I’ll be attending PDC in Los Angeles speaking about using MEF in Silverlight.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a href="http://microsoftpdc.com/Sessions/FT24"&gt;Building Extensible Rich Internet Applications with the Managed Extensibility Framework&lt;/a&gt;&amp;#160;&lt;/p&gt;    &lt;p&gt;In Hall F on Thursday at 11:30 AM&lt;/p&gt;    &lt;p&gt;Learn how the Microsoft .NET Framework 4 introduces the Managed Extensibility Framework (MEF) for building customizable applications that can easily be extended by third parties. Whether you are building an extensible data grid, a custom rules engine, a pluggable editor, or a composite application such as a pluggable CRM system, you want to learn about MEF. Hear how to use MEF to decouple your applications into more maintainable and testable pieces, and partition your application into dynamically deployable chunks that download on-demand.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;If you haven’t heard, &lt;a href="http://mef.codeplex.com"&gt;MEF&lt;/a&gt; is available in Silverlight similar to it’s availability on the desktop.&amp;#160; But what does it mean to build an extensible app in Silverlight? Yes it’s technically possible, but why should you care? In this talk we’ll explore different application scenarios for when MEF is a good fit. We’ll also explore some of the SL specific features we’ve introduced in order to enrich the MEF experience.&lt;/p&gt;  &lt;p&gt;Hope to see you there!&lt;/p&gt;  &lt;p&gt;Glenn &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9923065" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/gblock/archive/tags/silverlight/">silverlight</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/RIA/">RIA</category></item><item><title>Should I use MEF with an IoC container? - Part 1</title><link>http://blogs.msdn.com/b/gblock/archive/2009/11/01/should-i-use-mef-with-an-ioc-container-part-1.aspx</link><pubDate>Sun, 01 Nov 2009 10:31:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9915829</guid><dc:creator>Glenn Block MSFT</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/gblock/rsscomments.aspx?WeblogPostID=9915829</wfw:commentRss><comments>http://blogs.msdn.com/b/gblock/archive/2009/11/01/should-i-use-mef-with-an-ioc-container-part-1.aspx#comments</comments><description>&lt;P&gt;In my post &lt;A href="http://codebetter.com/blogs/glenn.block/archive/2009/08/16/should-i-use-mef-for-my-general-ioc-needs.aspx" mce_href="http://blogs.msdn.com/blogs/glenn.block/archive/2009/08/16/should-i-use-mef-for-my-general-ioc-needs.aspx"&gt;"Should I use MEF for my IoC needs"&lt;/A&gt;&amp;nbsp;we took a look at the question of whether or not it is appropriate to use MEF instead of a traditional&amp;nbsp;IoC container. In this post we'll discuss the question of whether or not you should use both an IoC container and MEF in the same application. &lt;/P&gt;
&lt;H2&gt;MEF and IoC container concerns&lt;/H2&gt;
&lt;P&gt;As we've mentioned in the past, MEF's primary design in V1&amp;nbsp;is&amp;nbsp;around open-systems whose components (parts) are dynamically discovered at runtime. IoC containers on the other hand are primarily&amp;nbsp;designed for&amp;nbsp;decoupling&amp;nbsp;a closed&amp;nbsp;(fixed) set of application components in order to&amp;nbsp;improve the maintainability of the system by the engineering team. In other words each is addressing a specific set of concerns. So if that is the case then is it plausable to use both in an application? The answer is &lt;STRONG&gt;Yes&lt;/STRONG&gt;. Not only that, but we specifically designed MEF with the idea of integration with other containers in mind. Below will explore one of the key-scenarios for doing this.&lt;/P&gt;
&lt;H2&gt;Enabling third-party extensibility in an IoC container-dependent application.&amp;nbsp;&lt;/H2&gt;
&lt;P&gt;You have an existing customer management system that uses an IoC container such as Castle Windsor which you use in order to promote separation-of-concens and testability. The application leverages advanced Windsor facilities for integrating with NHibernate and for applying cross-cutting-concerns (AOP) such as logging, etc. The loosely coupled design of the system has proven to be greatly beneficial and has minimized the friction for developing and deploying the system through several releases. &lt;/P&gt;
&lt;P&gt;At some point requests start pouring in&amp;nbsp;from customers want to create their own extensions in order to customize the application in their specific environments. Additionally several ISVs express interest in providing third-party&amp;nbsp;value-adds such as telephony integration, and scheduling.&amp;nbsp;&amp;nbsp;You determine that adding a third-party&amp;nbsp;extensibility model to the application makes sense in order to address this opportunity. Through your&amp;nbsp;research you come across MEF and realize that it provides the infrastructure you need for extensibility.&amp;nbsp; However&amp;nbsp;it does so&amp;nbsp;in a manner that&amp;nbsp;overlaps with&amp;nbsp;many of the&amp;nbsp;functions that Windsor is providing.&amp;nbsp;What to do? Let's explore various options.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;a. Migrate the existing code to use MEF and remove&amp;nbsp;the IoC container&amp;nbsp;completely.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;One option is to consider ripping out Castle and just using MEF. Technically this&amp;nbsp;sounds reasonable, however after investigation&amp;nbsp;it is not feasible. There are literally thousands&amp;nbsp;of components that are currently registered in the Windsor container.&amp;nbsp;Moving these to MEF&amp;nbsp;involves modfying all of these existing classes&amp;nbsp;in order to make them discoverable by MEF, not to mention refactoring all the places in the application where the container is accessed.&amp;nbsp; Because the changes are so widespread, the likelihood of introducing&amp;nbsp; breakages / bugs in the system is very high. Additionally there are the various Castle facilities such as Dynamic Proxy, and NHibernate integration&amp;nbsp;which MEF does not yet support, which will have to authored by the team. The cost / risk of such changes is too prohibitive, not to mention that it seems&amp;nbsp;like it should be unnecessary.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;b. Implement a custom&amp;nbsp;extensibility mechanism&amp;nbsp;built on top of the&amp;nbsp;IoC container?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Another option is to author a home-grown solution. This can be a viable option depending on the needs, and the willingness to author your own solution. For simple cases you can easily facade an extensibility mechanism on top of an IoC. Once you start getting into more advanced features like lazy instantiation, metadata, recomposition and stable composition, you are in for quite a bit of work. &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;c. Have&amp;nbsp;both&amp;nbsp;the IoC&amp;nbsp;and MEF both present in the same application?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;The final option I want to discuss (and the one I recommend for these situations) is to use both. Use the IoC container for addressing internal application decoupling needs, and use MEF for third-party extensibiltiy needs, that is the IoC container for IoCish things and the MEF container for MEFfish things :-)&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This path is appealing for several reasons. &lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;It works with existing codebases whether they be applications or frameworks. it does not require any significant rewrite.&lt;/LI&gt;
&lt;LI&gt;It allows leveraging the strengths of each container for the things it does best. For example leveraging MEF's metadata model for discriminating between different extensions vs the dynamic proxy support in Castle. &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;However, there are several caveats of this approach.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;It&amp;nbsp;raises questions such as: 
&lt;UL&gt;
&lt;LI&gt;Which component (part) should live in which container? &lt;/LI&gt;
&lt;LI&gt;Who owns the lifetime?&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;Can a MEF part access services coming from the IoC?&amp;nbsp; How?&lt;/LI&gt;
&lt;LI&gt;Can the IoC access exports coming from MEF?&amp;nbsp;&amp;nbsp;How?&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;
&lt;LI&gt;It increases complexity in that now there are components living in mulitple places. &lt;/LI&gt;
&lt;LI&gt;It increases the potential points of failuire within the application. &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;All of these issues are managable. In my next post we'll see how.&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9915829" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/gblock/archive/tags/ioc/">ioc</category><category domain="http://blogs.msdn.com/b/gblock/archive/tags/MEF/">MEF</category></item></channel></rss>
