title: MEF Import - Object reference error
body: <p>I'm trying to figure out why one of my objects does not seem to be satisfying its Import. I think it may be the way I am using the container.ComposeParts() function but I have not been able to find much documentation with regards to it.</p>
  
  <p>I have four projects<br>
  MEF.Service.Contracts<br>
  MEF.Service.Messaging (implements contracts)<br>
  MEF.Service.Core<br>
  MEF.Service.Console</p>
  
  <p>MEF.Service.Contracts contains a simple interface called IMessageService with one function  </p>
  
  <pre><code>public interface IMessageService
  {
      void SendMessage(string message);
  }
  </code></pre>
  
  <p>MEF.Service.Messaging has one implementation of the above  </p>
  
  <pre><code>[Export(typeof(IMessageService))]
  public class ConsoleMessageService : IMessageService
  {
      #region IMessageService Members
  
      public void SendMessage(string message)
      {
          Console.WriteLine(message);
      }
  
      #endregion
  }
  </code></pre>
  
  <p>MEF.Service.Core has a class called ServiceManager that imports available services. In this example the IMessageService  </p>
  
  <pre><code>public class ServiceManager
  {
      [Import]
      public IMessageService MessageService { get; set; }
  }
  </code></pre>
  
  <p>Finally, in the MEF.Service.Console application project I am creating the MEF container using a DirectoryCatalog. Then I create an instance of ServiceManager and call its MessageService property.<br>
  However at this point it fails with an object reference error.  </p>
  
  <p>Here is the code in the MEF.Service.Console project  </p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          Program p = new Program();
          p.Run();
      }
  
      public void Run()
      {
          Compose();
  
          ServiceManager manager = new ServiceManager();
          manager.MessageService.SendMessage("Test Message");
  
          Console.ReadLine();
      }
  
      private void Compose()
      {
          var catalog = new DirectoryCatalog(@".\");
          var container = new CompositionContainer(catalog);
          container.ComposeParts(this);
      }
  }
  </code></pre>
  
  <p>MEF.Service.Console has references to the other three projects just to ensure the dlls are in the same folder during runtime.</p>
  
  <p>I've examined the catalog and container after they are initialized and it does contain my ConsoleMessageService export as a part.</p>
  
  <p>I'm trying to figure out why my [Import] MessageService in my ServiceManager is not getting satisfied.</p>
  
  <p>Any help or pointers on composing parts and how Imports get satified would be appreciated.</p>