title: ASP.NET MVC2 and MEF - Why can't my MefControllerFactory get exports or MetaData?
body: <p>I am following this blog post: <a href="http://blog.maartenballiauw.be/post/2009/04/21/ASPNET-MVC-and-the-Managed-Extensibility-Framework-%28MEF%29.aspx" rel="nofollow">http://blog.maartenballiauw.be/post/2009/04/21/ASPNET-MVC-and-the-Managed-Extensibility-Framework-%28MEF%29.aspx</a> and I'm having dificulty implementing the <code>MefControllerFactory</code>.</p>
  
  <h3>MefControllerFactory Code</h3>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.ComponentModel.Composition;
  using System.ComponentModel.Composition.Hosting;
  using System.Web.Mvc;
  
  namespace plugme.Utilities
  {
  
      public class MefControllerFactory : IControllerFactory
      {
          private string pluginPath;
          private DirectoryCatalog catalog;
          private CompositionContainer container;
  
          private DefaultControllerFactory defaultControllerFactory;
  
          public MefControllerFactory(string pluginPath)
          {
              this.pluginPath = pluginPath;
              this.catalog = new DirectoryCatalog(pluginPath);
              this.container = new CompositionContainer(catalog);
  
              this.defaultControllerFactory = new DefaultControllerFactory();
          }
  
          #region IControllerFactory Members
  
          public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
          {
              IController controller = null;
  
              if (controllerName != null)
              {
                  string controllerClassName = controllerName + "Controller";
  
                  // "Export" isn't recognized
                  // and "Metadata" (as in c =&gt; c.Metadata ) isn't recognized.
                  Export&lt;IController&gt; export = this.container.GetExports&lt;IController&gt;()
                                                   .Where(c =&gt; c.Metadata.ContainsKey("controllerName")
                                                       &amp;&amp; c.Metadata["controllerName"].ToString() == controllerName)
                                                   .FirstOrDefault();
                  if (export != null)
                  {
                      controller = export.GetExportedObject();
                  }
              }
  
              if (controller == null)
              {
                  return this.defaultControllerFactory.CreateController(requestContext, controllerName);
              }
  
              return controller;
          }
  
          public void ReleaseController(IController controller)
          {
              IDisposable disposable = controller as IDisposable;
              if (disposable != null)
              {
                  disposable.Dispose();
              }
          }
  
          #endregion
      }
  }
  </code></pre>
  
  <p>The errors I'm getting are:</p>
  
  <pre><code>Error 1 The type or namespace name 'Export' could not be found 
          (are you missing a using directive or an assembly reference?)   
  
  Error 2 'System.Lazy&lt;System.Web.Mvc.IController&gt;' does not contain a 
          definition for 'Metadata' and no extension method 'Metadata' 
          accepting a first argument of type 
          'System.Lazy&lt;System.Web.Mvc.IController&gt;' could be found 
          (are you missing a using directive or an assembly reference?)
  
  Error 3 'System.Lazy&lt;System.Web.Mvc.IController&gt;' does not contain a 
          definition for 'Metadata' and no extension method 'Metadata' 
          accepting a first argument of type 
          'System.Lazy&lt;System.Web.Mvc.IController&gt;' could be found 
          (are you missing a using directive or an assembly reference?)   
  </code></pre>
  
  <p>I'm rather confused as to why this wouldn't recognize <code>Export</code> or <code>Metadata</code>. Do you guys have any thoughts?</p>
  
  <h2>Edit</h2>
  
  <p>I changed the line:</p>
  
  <pre><code> Export&lt;IController&gt; export = this.container.GetExports&lt;IController&gt;()
                                                   .Where(c =&gt; c.Metadata.ContainsKey("controllerName")
                                                       &amp;&amp; c.Metadata["controllerName"].ToString() == controllerName)
                                                   .FirstOrDefault();
  </code></pre>
  
  <p>To:</p>
  
  <pre><code>var export = this.container.GetExports&lt;IController&gt;()
                                                   .Where(c =&gt; c.Metadata.ContainsKey("controllerName")
                                                       &amp;&amp; c.Metadata["controllerName"].ToString() == controllerName)
                                                   .FirstOrDefault();
  </code></pre>
  
  <p>That took care of my issues with <code>Metadata</code>. But now i hev a new error with the next if statement:</p>
  
  <pre><code>            if (export != null)
              {
                  controller = export.GetExportedObject(); 
              }
  </code></pre>
  
  <p>error:</p>
  
  <pre><code> 'System.Lazy&lt;System.Web.Mvc.IController,System.Collections.Generic.IDictionary&lt;string,object&gt;&gt;' does not contain a definition for 'GetExportedObject' and no extension method 'GetExportedObject' accepting a first argument of type 'System.Lazy&lt;System.Web.Mvc.IController,System.Collections.Generic.IDictionary&lt;string,object&gt;&gt;' could be found (are you missing a using directive or an assembly reference?)
  </code></pre>