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

Stable Composition question

Aug 20, 2009 at 3:20 PM

Lets say there is a contract IService and several implementations for it Service1, Service2 etc

following would couse part reject: [import(typeof(IService))] IService{get;set;}

Is there a way to provide MEF with method it should use to resolve ambigy of this kind?




Aug 20, 2009 at 4:01 PM


There's no universal mechanism for doing this. MEF components 'configure themselves' - there's no external place to select between different implementations except via the catalogs.

The recommended approach in this case is to use ImportMany and select the implementation based on metadata.

You might instead consider either filtering the catalog or filtering the types that go into the catalog.

Another option is to set up a custom container topology - the ExportProviders passed to the constructor are checked in order and any ExactlyOne imports will be resolved by the first ExportProvider that supplies the part.


Aug 20, 2009 at 5:21 PM

Depending on your scenario using ImportMany with an custom collection similar to the AdaptingCollection mentioned by Glen at may also provide you with an option.

Aug 20, 2009 at 6:50 PM

Also I have a blog post here discussing this issue and some options to consider.

Aug 21, 2009 at 2:56 PM

Looks like there is no painless solution for my case (I need to have named implementations of contracts and be able to collate them over plugin instances following from config file proclaming smth like 'when instantiating plagin A use instance named 'instanceB' for contract  Ib').

So the next question: is it possible to use MEF only to explore dlls. In other words is it possible to query MEF for all types implementing given contract?