MEF: How to resolve a type within the Module?

Topics: Prism v4 - Silverlight 4
Jan 9, 2011 at 10:13 PM

MEF is still a mystery to me.  

With Unity I could have an IUnityContainer injected into my module's constructor and from there I would simply register my IService with a Service type by utilizing the UnityContainer.

I couldn't find a single MEF example how to do this within MEF driven Modules.  Modules don't get an equivalent to IUnityContainer injected and hence the service can't be resolved within the module.

In a way it seems MEF is used within the BootStrapper class only, while the container that registers everything remains referenced in there only. Other Modules don't have access to it and can't resolve anything.

How do I register my IService with Service type and resolve it within a module please?  Any code snippets? 

Many Thanks for clarification,


Jan 10, 2011 at 5:14 AM

MEF is initialized from the MEF Bootstrapper from there you can do everything that Unity can do.  Now for Resolving  types...

As long as the class is attributed with the [Export] or the Variable is tagged with [Import] then it will be resolved.  If it can't find a matching item in the container dictionary it will explode on run time and throw errors for you try and interpret. 

[Export(typeof(IService))]   // typeof(IService) suggested not required, but to be resolved in the Container it must have the [Export] attribute.
public class Service : IService{

  public Service(){ }

   //do what ever here..


public interface IService{

   // stuff here if necessary.



//ViewModel sample code.

public class SomeViewModel : NotificationObject{

   private IService service;

   public SomeViewModel (IService service){

       this.service = service;





Jan 17, 2011 at 11:08 AM



Many Thanks for your response. I was still stuck with some other issues in Prism that I had to overcome first.

Now while this makes sense, what if i have two implementations for the IService (Obviously to mock it for unit testing). Both Servies would be exported for the type of interface: [Export(typeof(IService))]

But how does it know which one to pick at the constructor injection within the view model?

Many Thanks,



Jan 17, 2011 at 4:10 PM


I guess the simplest answer would be use ExportMetaData or even to go as far as strongly typing your exports

Examples can be found here:



Jan 18, 2011 at 4:34 PM
Edited Jan 18, 2011 at 4:43 PM

Hi Morgan,

Thanks for the tip I have looked into it, but I still don't see the light.

Looking at Shawn Wildermuth example here:

He has an [Export] ViewModel that consumes a [Import]Model.

In his unit test class he is retrieving a new instance (non Shared) of the ViewModel and does his unit testing.

However how does MEF magically know that it has to inject the MockModel inside the ViewModel instead of the GamesModel? There are no attributes as such. Both GamesModel and MockModel implement only the IGamesModel interface.  The Export happens to be [Export(typeof(IGamesModel))] ob both models. No indication of distinctive attributes between the two implementations.