title: Using MEF with the Enterprise Library
body: <p>I'm trying to override the default Unity Container behavior for the Enterprise Library so that I can use my MEF container. There are a few resources with explanations of how to do this but I'm just not getting it:</p>
  
  <ul>
  <li><a href="http://blogs.msdn.com/b/bobbrum/archive/2009/06/23/enterprise-library-5-0-some-architecture-changes.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/b/bobbrum/archive/2009/06/23/enterprise-library-5-0-some-architecture-changes.aspx</a></li>
  <li><a href="http://entlib.uservoice.com/forums/90505-silverlight-integration-pack/suggestions/1284693-mef-configurator-for-enterprise-library-container" rel="nofollow noreferrer">http://entlib.uservoice.com/forums/90505-silverlight-integration-pack/suggestions/1284693-mef-configurator-for-enterprise-library-container</a></li>
  <li><a href="http://entlib.codeplex.com/discussions/261443" rel="nofollow noreferrer">http://entlib.codeplex.com/discussions/261443</a></li>
  </ul>
  
  <p>There is also this post on SO but the code does not compile because <code>LogWriter</code> is protected. I assume this referred to an old version:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/5637678/net-mef-with-enterprise-library-5-0">.net Mef with Enterprise Library 5.0</a></li>
  </ul>
  
  <p>What I understand is that I need to use the <code>CommonServiceLocator</code> for my MEF container and then attach this to the Enterprise Library container. Here's what I have for my container configurator:</p>
  
  <pre><code>public class MefContainerConfigurator : IContainerConfigurator, IServiceLocator
  {
      [Import] private CatalogExportProvider provider;
  
      public object GetService(Type serviceType)
      {
              throw new NotImplementedException();   
      }
  
      public object GetInstance(Type serviceType)
      {
          return provider.GetExportedValue&lt;Type&gt;();
      }
  
      public object GetInstance(Type serviceType, string key)
      {
          throw new NotImplementedException();
      }
  
      public IEnumerable&lt;object&gt; GetAllInstances(Type serviceType)
      {
          throw new NotImplementedException();
      }
  
      public TService GetInstance&lt;TService&gt;()
      {
          return provider.GetExportedValue&lt;TService&gt;();
      }
  
      public TService GetInstance&lt;TService&gt;(string key)
      {
          return provider.GetExportedValue&lt;TService&gt;(key);
      }
  
      public IEnumerable&lt;TService&gt; GetAllInstances&lt;TService&gt;()
      {
          return provider.GetExportedValues&lt;TService&gt;();
      }
  
      public void RegisterAll(IConfigurationSource configurationSource, ITypeRegistrationsProvider rootProvider)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p>And in my bootstrapper:</p>
  
  <pre><code>var configurator = new MefContainerConfigurator();
  // Does this line read the Enterprise Library configuration from the app.config?
  IConfigurationSource cs = new SystemConfigurationSource();
  
  EnterpriseLibraryContainer.ConfigureContainer(configurator, cs);
  </code></pre>
  
  <p>I think maybe I need to make use of the <code>LogWriterImpl</code> and <code>ExceptionManagerImpl</code> classes as these have constructors which accept configuration. My questions at this point would be:</p>
  
  <ul>
  <li>How do I retrieve the configuration from the <code>IConfigurationSource</code> and feed it into the constructors for the <code>LogWriterImpl</code> and <code>ExceptionManagerImpl</code> constructors?</li>
  <li><code>EnterpriseLibraryContainer.ConfigureContainer</code> calls <code>RegisterAll</code> in my <code>MefContainerConfigurator</code>. Is this where I'm supposed to register all of the enterprise library types into the container?</li>
  <li>The methods from the <code>IServiceLocator</code> interface that I have left as NotImplemented; I couldn't find a way to use these to return objects from my container. Am I supposed to leave them as not implemented and use the generic methods instead?</li>
  </ul>
  
  <p><strong>Edit</strong></p>
  
  <p>I still cannot get this totally right. Based on @Chris Tavares answer, I wrote my <code>RegisterAll</code> method in the <code>MefContainerConfigurator</code> to iterate through the TypeRegistrations and add them to a container. I cannot for the life of me figure out how to merge these to my <code>AggregateContainer</code> that is created in my <code>Bootstrapper</code> class so that I can actually use these exports outside of the <code>ContainerConfigurator</code>:</p>
  
  <pre><code>public void RegisterAll(IConfigurationSource configurationSource, ITypeRegistrationsProvider rootProvider)
  {
      var registrations = rootProvider.GetRegistrations(configurationSource);
  
      foreach (var type in registrations)
      {
          var builder = new RegistrationBuilder();
  
          builder.ForType(type.ServiceType).Export();
  
          var cat = new AssemblyCatalog(type.ServiceType.Assembly, builder);
          var container = new CompositionContainer(cat);
          container.ComposeParts(this);
      }
  }
  </code></pre>
  
  <p>ConfigureAggregateCatalog in Prism bootstrapper:</p>
  
  <pre><code>protected override void ConfigureAggregateCatalog()
  {
      AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(RegionNames).Assembly));
      // Module assemblies
      AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(DataEntryModule).Assembly));
      AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ReportingModule).Assembly));
      AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(StatusBarModule).Assembly));
      AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(SplashScreenModule).Assembly));
      AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(WelcomeModule).Assembly));
      AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(AdministrationModule).Assembly));
  
      AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
  }
  </code></pre>