title: Custom Service Locator with MEF and IOC container from galasoft
body: <p>I've a requirement where I've to support two types of container in my application. Therefore I've created a custom ServiceLocator that serves 2 types of containers i.e.</p>
  
  <ol>
  <li><p><strong>SimpleIoc (Galasoft)</strong> </p></li>
  <li><p><strong>MefServiceLocatorAdapter</strong></p></li>
  </ol>
  
  <p>Therefore I've created a common service locator class i.e. </p>
  
  <pre><code>public class CommonServiceLocator : ServiceLocatorImplBase
  {
      private IServiceLocator _iocContainer, _mefContainer;
  
      public CommonServiceLocator(IServiceLocator iocLocator, IServiceLocator mefLocator)
      {
          _iocContainer = iocLocator;
          _mefContainer = mefLocator;
      }
  
      protected override IEnumerable&lt;object&gt; DoGetAllInstances(Type serviceType)
      {
  
          IEnumerable&lt;object&gt; services;
          try
          {
              services = _iocContainer.GetAllInstances(serviceType);
          }
          catch (Exception)
          {
              //Current assumption is that if IocContainer doesn't contain an instance then
              //it should look for MefContainer for values
              services = _mefContainer.GetAllInstances(serviceType);
          }
          return services;
      }
  
      protected override object DoGetInstance(Type serviceType, string key)
      {
          object service;
          try
          {
              service = _iocContainer.GetInstance(serviceType, key);
          }
          catch (Exception)
          {
              //Current assumption is that if IocContainer doesn't contain an instance then
              //it should look for MefContainer for values
              service = _mefContainer.GetInstance(serviceType, key); //&lt;== This fails to get an instance
          }
          return service;
      }
  
      public override object GetInstance(Type serviceType, string key)
      {
          return DoGetInstance(serviceType, key);
      }
  
      public override object GetInstance(Type serviceType)
      {
          return GetInstance(serviceType, null);
      }
  }
  </code></pre>
  
  <p>Now, the problem is when I try to get a class instance through IOC container using GetInstance() method it works fine. However, obtaining a class instance through MefContainer throws some error ("<em>A first chance exception of type 'System.InvalidOperationException' occurred in System.Core.dll</em>"). I've created a test class to test this scenario: </p>
  
  <pre><code>public class ServiceLocatorTest
  {
      public void CommonServiceLocatorInstanceTest()
      {
          var serviceLocator = new SimpleIoc();
          serviceLocator.Register&lt;GalaSoftIocData&gt;();
  
          AggregateCatalog catalog = new AggregateCatalog();
          catalog.Catalogs.Add(new AssemblyCatalog(typeof(MefCompositionAddin).Assembly));
          CompositionContainer compContainer = new CompositionContainer(catalog);
  
          CompositionBatch batch = new CompositionBatch();
  
          batch.AddPart(AttributedModelServices.CreatePart(new MefCompositionAddin()));
  
          compContainer.Compose(batch);
  
          var vl = compContainer.GetExportedValue&lt;MefCompositionAddin&gt;(); // &lt;== This gives instance correctly
          var mefserviceLocator = new MefServiceLocatorAdapter(compContainer);
          var commonServiceLocator = new CommonServiceLocator(serviceLocator, mefserviceLocator);
          var galaSoftData = commonServiceLocator.GetInstance(typeof(GalaSoftIocData));
          var mefData = commonServiceLocator.GetInstance(typeof(MefCompositionAddin));
      }
  }
  
  [Export]
  class MefCompositionAddin
  {
  
      public string MyData { get { return "Mef's Data composed"; } }
  
      [Import]
      public MefCompositionAddin MyObj { get; set; }
  }
  
  class MefCompositionData
  {
      [Import]
      public MefCompositionAddin MyAddin { get; set; }
  }
  
  class GalaSoftIocData
  {
      public string MyData { get { return "Galasoft's Data composed"; } }
  }
  </code></pre>
  
  <p>Kindly let me know if i've missed anything. and what could be the possible cause. Thanks everyone for your help in advance. </p>