This project has moved and is read-only. For the latest updates, please go here.

Multiple ImportMany for different contracts

Dec 17, 2010 at 4:24 PM

Hi everyone.  I'm using the MEF that ships with .NET v4, and am trying to compose the following situation:

      private CompositionContainer _mefCompositionContainer = null;

      public IEnumerable<Lazy<IReader>> ReaderPlugins { get; set; }

      public IEnumerable<Lazy<IWriter>> WriterPlugins { get; set; }

         // some code removed for clarity
         DirectoryCatalog readerCatalog = new DirectoryCatalog(readerFolder);
         DirectoryCatalog writerCatalog = new DirectoryCatalog(writerFolder);
         AggregateCatalog catalog = new AggregateCatalog(readerCatalog, writerCatalog);

         // Get MEF to compose the parts in the aggregate catalog
         _mefCompositionContainer = new CompositionContainer(catalog);

This seems to work fine, although I have noticed both DirectoryCatalogs contain all assemblies with Exports (I have two test assemblies, one exporting the reader type and the other exporting the writer type, but they live in the same directory so each catalog contains both).  That makes sense, but it does mean my Catalog.Parts.Count == 4 (not 2).  The reason I'm doing this is the reader and writer plugins could be configured to live in separate directories, but for now they're both pointing at the same place.

The problem I'm having is the accessors return nothing when I enumerate them.  I'm very new to MEF, so am hoping I've simply missed something obvious that someone can explain.  Failing that, the debugging guide didn't really help me much, so any other pointers would be much appreciated.

All help is very gratefully received!



Dec 17, 2010 at 5:27 PM

Having digested the debugging pages in much more detail, it turns out using CompostionInfo was very helpful as it showed I'd mixed my contracts up.  After fixing the problems with the Exports to ensure the contracts were correct, the accessor returned the plugins I expected :-)