When the MEF sees an import of type ExportFactory<IFoo> , it considers this in a special way. Instead of looking literally at the ExportFactory<IFoo> export, it looks instead of the IFoo export and magically generates a factory for this type.
Your mistake is that you expect this magic to automatically work for your own alternative ExportFactory , which you called SrviceProviderFactory . It is not true. When you import SrviceProviderFactory<IFoo,IFooMetadata> somewhere, MEF literally looks for an export of this type.
A simple solution is to give it this export. Manually export the factory for each implementation of IServiceProvider. For example, if you have a FooServiceProvider :
public class FooServiceProvider : IServiceProvider { public FooServiceProvider(Dependency dependency) { ... } }
Then you will also need FooServiceProviderFactory:
[Export(typeof(IServiceProviderFactory))] [ExportMetaData("foo", "bar")] public class FooServiceProviderFactory : IServiceProviderFactory { public IServiceProvider CreateServiceProvider(Dependency d) { return new FooServiceProvider(d); } }
And then your importer can choose the right factory based on metadata:
public class FactoryUser { [ImportMany] public Lazy<IServiceProviderFactory,IDictionary<string,object>>[] Factories { get; set; } public void DoSomething() { var factory = Factories.First(x => x.Metadata["foo"] == "bar").Value; var serviceProvider = factory.CreateServiceProvider(someDependency); ... } }
Itβs annoying that for each service provider implementation you also need to create and export a factory implementation. You can save your work by creating a common factory base class (for example, your SrviceProviderFactory ), but you still have to output certain classes because you cannot use type type parameters in an MEF export. update . I'm sure .NET 4.5 now supports exporting open generic types.
That's why I already suggested you export Func instead , but it seems you didnβt like this answer.
You can also try to reproduce the magic of ExportFactory . It is possible, but a very advanced use case for MEF. If you want to do this, I suggest you take a look at the sources of the MEF ExportFactoryProvider to learn how to create your own implementation with parameter support.