Property import using MEF in Prism

Topics: Prism v4 - WPF 4
Feb 4, 2011 at 11:52 AM


I am facing a weirdo problem while trying to perform a property import using MEF in Prism sample app. I am posting my sample snippet,

// Custom bootstrapper class

 public partial class MyBootstrapper : MefBootstrapper
       protected override DependencyObject CreateShell() { // returns shell }
       protected override void  InitializeShell() {// inti shell }  

       protected override void ConfigureAggregateCatalog() {
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()));
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog((typeof(MyModuleOne).Assembly)));  }

        protected override void ConfigureContainer() { base.ConfigureContainer(); }    

Now my little MyModuleOne has this,

    public class MyModuleOne : IModule
        public MyService myservice { get; set; } // This is the service I want to property import

        public void Initialize() { myservice.DoSomething; }

Now Service class is,


class MyService { // Function DoSomething }

Now I am unable get the 'myservice' populated. It always returning me NULL!!

Am I missing something obvious. Can anyone throw light on property import concept from Prism view point.


Thanks in advance.



Feb 4, 2011 at 12:50 PM

Your Service must be public.



Feb 4, 2011 at 1:30 PM


Take into account that MyService class needs to be public and having a default constructor.

In the other hand, it might be convenient to inject MyService in the constructor of your module. For example:

public MyModuleOne(MyService service)
   this.myService = service
private MyService myService;


Note that, when MEF composes your class, injecting dependencies through the use of an importing constructor, each of the dependencies injected is also composed by MEF.

Also, note that every class that will be composed by MEF must be marked as an export, by decorating it with the Export attribute. You can read more about this in the following articles from the MEF Programming Guide:

Also you can read Chapter 3: Managing Dependencies Between Components from Prism documentation.

Please let me know if this information helps you.


Miguel Bronzovic


Feb 7, 2011 at 3:35 AM

Hey Miguel,

Thanks for the reply. I have my service class as public (missed it earlier - typo) and also provided a default constructor.

But still my property import does not work! I have tried [ImportingConstructor] earlierm which worked perfectly fine. But I am not able to get the issue with Property import.

Would it possible for you to share some sample snippets showing property import? That will be great for me to get an insight into the rationale.