Overriding the host configuration

MEF creates a default host configuration for CompositionInitializer the first time
SatisfyImports is called. This is ideal either for getting off the ground with MEF, or for simple applications which simply discover all parts in their current XAP. For more complex scenarios such as
application partitioning we offer the System.ComponentModel.Composition.Hosting.CompositionHost class (in System.ComponentModel.Composition.Initialization.dll) which allows you to override the configuration. To use
CompositionHost you call its initialize method in your app startup and specify your own configuration which MEF will use.

Note: CompositionHost.Initialize can only be called a single time within the app. Future calls with throw an exception. We also recommend that only the host calls this code rather than 3rd party parts.

Overriding with catalogs

The simplest way to override the config is to call the overload of Initialize which accepts catalogs. MEF then creates a container behind the scenes that uses those catalogs. For example below I am overriding the config to pass in an AggregateCatalog which
I can use to add other catalogs in the future.

In the code above the application is calling Initialize passing in an AggregateCatalog and an assembly catalog for the current assembly. SatisfyImports is then called which causes the ViewModel to get imported.

Configuring to discover parts in the current XAP.

When you override you take full control, MEF does not assume anything about the config. This means the parts in the main XAP are not discovered. For example above we passed in an assembly catalog with the current assembly. If you want the main XAP to be discovered
you can pass in a DeploymentCatalog created with the default constructor. This tells MEF "find all parts in the current XAP". Below is how our startup code looks if we use this.

Overriding with a container

In general overrding with catalogs should be sufficient. For more advanced scenarios such as providing a scoped container strategy you may want to override the container itself. To do this you can call the override of
Initialize that accepts a container. Below is how the startup code above changes if we do that.

Comments

I've got this error with the code above.:Error 1 The best overloaded method match for 'System.ComponentModel.Composition.Hosting.CompositionContainer.CompositionContainer(System.ComponentModel.Composition.Primitives.ComposablePartCatalog, bool, params System.ComponentModel.Composition.Hosting.ExportProvider[])' has some invalid arguments

The project reference System.ComponentModel.Composition and System.ComponentModel.Composition.Initialization. The usings are System.ComponentModel.Composition and System.ComponentModel.Composition.Hosting.Am I missing a using or reference ?