<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>IIrrelevant - MEF</title>
    <link>http://www.milkcarton.com/blog/</link>
    <description>Irrelevant musings about software development</description>
    <language>en-us</language>
    <copyright>Dan Morphis</copyright>
    <lastBuildDate>Sat, 04 Apr 2009 05:37:33 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>blog@milkcarton.com</managingEditor>
    <webMaster>blog@milkcarton.com</webMaster>
    <item>
      <trackback:ping>http://www.milkcarton.com/blog/Trackback.aspx?guid=bcd01da5-498e-410c-a687-acc721220457</trackback:ping>
      <pingback:server>http://www.milkcarton.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.milkcarton.com/blog/PermaLink,guid,bcd01da5-498e-410c-a687-acc721220457.aspx</pingback:target>
      <dc:creator>Dan Morphis</dc:creator>
      <wfw:comment>http://www.milkcarton.com/blog/CommentView,guid,bcd01da5-498e-410c-a687-acc721220457.aspx</wfw:comment>
      <wfw:commentRss>http://www.milkcarton.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=bcd01da5-498e-410c-a687-acc721220457</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <h3>Background
</h3>
        <p>
For a project I'm doing, I have a task model for the various pieces. In the beginning,
I was manually creating a List&lt;ITask&gt;. As I kept adding tasks to run, I started
thinking about hacking some code together to rifle through my assembly and pull back
all the classes which implement ITask.
</p>
        <p>
Then I remember hearing about Managed Extensibility Framework (MEF). I did some searching,
found the <a href="http://www.codeplex.com/MEF/" target="_blank">MEF home page</a>,
and even read the <a href="http://mef.codeplex.com/Wiki/View.aspx?title=Overview&amp;referringTitle=Home" target="_blank">MEF
overview</a>. But none of that told me what I really wanted to know, what's the fastest
way to get started using MEF as a component loader?
</p>
        <p>
I did some more searching and found the <a href="http://dnrtv.com/" target="_blank">dnrTV</a> episode
"<a href="http://www.dnrtv.com/default.aspx?showNum=130" target="_blank">Glenn Block
on MEF, the Managed Extensibility Framework</a>" and after 20-30 minutes they finally
got down to how to create a plugin for your app.
</p>
        <p>
But what I really wanted, and I bet a lot of others, is a quick start guide for creating
a plugin.
</p>
        <h3>Solution
</h3>
        <p>
          <a href="http://mef.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=22313" target="_blank">Download
the latest version of MEF</a>, as of this writing its Preview 4. Grab the System.ComponentModel.Composition.dll
from the bin folder and stash it somewhere. Make a reference to said dll in your project.
</p>
        <p>
On your plugin class, add Export attribute:
</p>
        <div>
          <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">[Export(<span style="color: #0000ff">typeof</span>(IPlugin))] <span style="color: #0000ff">public</span><span style="color: #0000ff">class</span> Foo
: IPlugin { ... }</pre>
        </div>
        <p>
In your plugin consumer, create a property to hold your plugins, and add the Import
attribute:
</p>
        <div>
          <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">[Import(<span style="color: #0000ff">typeof</span>(IPlugin))] <span style="color: #0000ff">internal</span> IList&lt;IPlugin&gt;
_myPlugins { get; set; }</pre>
        </div>
        <p>
Now, tell MEF where to get the plugins at (line 2), and where you want MEF to fulfill
any plugins (line 5):
</p>
        <div>
          <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
            <p>
              <span style="color: #0000ff">private</span>
              <span style="color: #0000ff">void</span> LoadPlugins()
{ var catalog = <span style="color: #0000ff">new</span> AssemblyCatalog(Assembly.GetExecutingAssembly());
var container = <span style="color: #0000ff">new</span> CompositionContainer(catalog);
var batch = <span style="color: #0000ff">new</span> CompositionBatch(); batch.AddPart(<span style="color: #0000ff">this</span>);
container.Compose(batch); } 
</p>
            <p>
 
</p>
          </pre>
        </div>
        <p>
I put my call to the LoadPlugins method in the constructor.
</p>
        <p>
Now, spin through your plugins and do the work:
</p>
        <div>
          <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">Console.WriteLine(<span style="color: #006080">"Found
{0} plugins"</span>, _myPlugins.Count); <span style="color: #0000ff">foreach</span> (var
plugin <span style="color: #0000ff">in</span> _myPlugins) { Console.WriteLine(plugin.Name);
}</pre>
        </div>
        <p>
 
</p>
        <p>
          <a href="/blog/content/binary/5_Minute_MEF_Tutorial.zip" target="_blank">Download
the complete source</a> to this (really, only about 10 extra lines to glue things
together) and have fun!
</p>
        <a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.milkcarton.com%2fblog%2f2009%2f04%2f04%2f5%2bMinute%2bTutorial%2bOn%2bManaged%2bExtensibility%2bFramework%2bMEF.aspx">
          <img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.milkcarton.com%2fblog%2f2009%2f04%2f04%2f5%2bMinute%2bTutorial%2bOn%2bManaged%2bExtensibility%2bFramework%2bMEF.aspx" border="0" alt="kick it on DotNetKicks.com" />
        </a>
        <img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=bcd01da5-498e-410c-a687-acc721220457" />
      </body>
      <title>5 Minute Tutorial on Managed Extensibility Framework (MEF)</title>
      <guid isPermaLink="false">http://www.milkcarton.com/blog/PermaLink,guid,bcd01da5-498e-410c-a687-acc721220457.aspx</guid>
      <link>http://www.milkcarton.com/blog/2009/04/04/5+Minute+Tutorial+On+Managed+Extensibility+Framework+MEF.aspx</link>
      <pubDate>Sat, 04 Apr 2009 05:37:33 GMT</pubDate>
      <description>&lt;h3&gt;Background
&lt;/h3&gt;
&lt;p&gt;
For a project I'm doing, I have a task model for the various pieces. In the beginning,
I was manually creating a List&amp;lt;ITask&amp;gt;. As I kept adding tasks to run, I started
thinking about hacking some code together to rifle through my assembly and pull back
all the classes which implement ITask.
&lt;/p&gt;
&lt;p&gt;
Then I remember hearing about Managed Extensibility Framework (MEF). I did some searching,
found the &lt;a href="http://www.codeplex.com/MEF/" target="_blank"&gt;MEF home page&lt;/a&gt;,
and even read the &lt;a href="http://mef.codeplex.com/Wiki/View.aspx?title=Overview&amp;amp;referringTitle=Home" target="_blank"&gt;MEF
overview&lt;/a&gt;. But none of that told me what I really wanted to know, what's the fastest
way to get started using MEF as a component loader?
&lt;/p&gt;
&lt;p&gt;
I did some more searching and found the &lt;a href="http://dnrtv.com/" target="_blank"&gt;dnrTV&lt;/a&gt; episode
"&lt;a href="http://www.dnrtv.com/default.aspx?showNum=130" target="_blank"&gt;Glenn Block
on MEF, the Managed Extensibility Framework&lt;/a&gt;" and after 20-30 minutes they finally
got down to how to create a plugin for your app.
&lt;/p&gt;
&lt;p&gt;
But what I really wanted, and I bet a lot of others, is a quick start guide for creating
a plugin.
&lt;/p&gt;
&lt;h3&gt;Solution
&lt;/h3&gt;
&lt;p&gt;
&lt;a href="http://mef.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=22313" target="_blank"&gt;Download
the latest version of MEF&lt;/a&gt;, as of this writing its Preview 4. Grab the System.ComponentModel.Composition.dll
from the bin folder and stash it somewhere. Make a reference to said dll in your project.
&lt;/p&gt;
&lt;p&gt;
On your plugin class, add Export attribute:
&lt;/p&gt;
&lt;div&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;[Export(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(IPlugin))] &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Foo
: IPlugin { ... }&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
In your plugin consumer, create a property to hold your plugins, and add the Import
attribute:
&lt;/p&gt;
&lt;div&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;[Import(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(IPlugin))] &lt;span style="color: #0000ff"&gt;internal&lt;/span&gt; IList&amp;lt;IPlugin&amp;gt;
_myPlugins { get; set; }&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Now, tell MEF where to get the plugins at (line 2), and where you want MEF to fulfill
any plugins (line 5):
&lt;/p&gt;
&lt;div&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
&lt;p&gt;
&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; LoadPlugins()
{ var catalog = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; AssemblyCatalog(Assembly.GetExecutingAssembly());
var container = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; CompositionContainer(catalog);
var batch = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; CompositionBatch(); batch.AddPart(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;);
container.Compose(batch); } 
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
I put my call to the LoadPlugins method in the constructor.
&lt;/p&gt;
&lt;p&gt;
Now, spin through your plugins and do the work:
&lt;/p&gt;
&lt;div&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;Console.WriteLine(&lt;span style="color: #006080"&gt;"Found
{0} plugins"&lt;/span&gt;, _myPlugins.Count); &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (var
plugin &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; _myPlugins) { Console.WriteLine(plugin.Name);
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="/blog/content/binary/5_Minute_MEF_Tutorial.zip" target="_blank"&gt;Download
the complete source&lt;/a&gt; to this (really, only about 10 extra lines to glue things
together) and have fun!
&lt;/p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.milkcarton.com%2fblog%2f2009%2f04%2f04%2f5%2bMinute%2bTutorial%2bOn%2bManaged%2bExtensibility%2bFramework%2bMEF.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.milkcarton.com%2fblog%2f2009%2f04%2f04%2f5%2bMinute%2bTutorial%2bOn%2bManaged%2bExtensibility%2bFramework%2bMEF.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=bcd01da5-498e-410c-a687-acc721220457" /&gt;</description>
      <comments>http://www.milkcarton.com/blog/CommentView,guid,bcd01da5-498e-410c-a687-acc721220457.aspx</comments>
      <category>.NET</category>
      <category>C#</category>
      <category>HowTo</category>
      <category>MEF</category>
    </item>
  </channel>
</rss>