Better MEF composition error diagnostics in .NET 4.5

Many of us suffered with obscure composition error messages of Managed Extensibility Framework. The most common error is ImportCardinalityMismatchException: No exports were found that match the constraint, but there are others. Much of the obscurity is caused by a feature called Stable Composition. It silently tries to compose all of your parts even if some of the imports are not satisfied. This leads to the actual errors to be buried deep into the composition graph, not to be seen in the thrown exception.

However, starting from .NET Framework 4.5, you can greatly improve the error messages by using new CompositionOptions enumeration, DisableSilentRejection option in particular. For example:


var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
var container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection);