Using MEF as primary IoC container

If you are going for Catel, we always recommend to use the ServiceLocator of Catel as primary IoC container. However, if you want to use MEF as the primary IoC container, follow the instructions below.

This is not a full starting guide to MEF, if you are looking for documentation about MEF, continue searching

Registering the container in Catel

It is required to register the external container in the ServiceLocator of Catel. This is required because all the items that are registered by Catel itself are known in the MEF container. Catel will automatically register all types in the MEF container when the container is registered. To register the container, simple use this call when the container is created:

ServiceLocator.Instance.RegisterExternalContainer(myMefContainer);

At this point, all the services registered by Catel are now registered in the MEF container.

Importing properties

To import services on a class, decorate the properties 

[Import]
public IPleaseWaitService PleaseWaitServiceViaMEF { get; set; }

 

The code above imports the IPleaseWaitService (which is internally registered by Catel) via MEF.

 Satisfying imports

After all imports are added, it is important to satisfy the imports using MEF. The imports can be satisfied via the following code:

myMefContainer.SatisfyImportsOnce(this);