Enabling ExportFactory<> (Glenn's desktop version) in PRISM

Topics: Prism v4 - WPF 4
Jan 21, 2012 at 1:15 AM


I'm trying to make use of ExportFactory<> functionality in the desktop version of WPF using PRISM. As ExportFactory<> is currently only available for Silverlight [MEF 2.0 will change this] I downloaded Glenn's desktop version which he posted over at http://mef.codeplex.com/discussions/207539?ProjectName=mef . Now to use this I need to register it with the catalog in the bootstrapper I assume.

Glen's version consists of a test which shows how to setup things:

        public void SetupMultipleFactories()
            var catalog = new TypeCatalog(typeof(PartA), typeof(PartB), typeof(PartThatUsesACollectionOfFactories));
            var ep = new ExportFactoryProvider();
            _container = new CompositionContainer(catalog, ep);
            ep.SourceProvider = _container;

I tried to translate this to PRISM's bootstrapper like so:

        protected override void ConfigureContainer()

            // Register ExportFactory
            var exportFactoryProvider = new ExportFactoryProvider();
            exportFactoryProvider.SourceProvider = Container;

But unfortunately it doesn't seem to work with this code:

    [ExportMetadata("Type", "Test1")]
    public class Test1 : TestBase{}

    [ExportMetadata("Type", "Test2")]
    public class Test2 : TestBase { }

    public class MainTestClass
        [ImportMany(AllowRecomposition = true)]
        public List<ExportFactory<TestBase, IDictionary<string,object>>> ViewModelFactory { get; set; }

When debugging I can see that the Count of ViewModelFactory is 0 where I expect it to be 2. I think this has to do with wrongly registering ExportFactory in the Bootloader. Anyone who can help? Much appriciated!


Jan 21, 2012 at 8:16 PM
Edited Jan 21, 2012 at 8:17 PM

Okay, I found out that the Providers property on the Catalog property in the Bootstrapper actually had Count = 0. This proved my assumption that the ExportFactoryProvider was not registered with the Catalog.

For me it was hard to figure out how to register it correctly, but after much of reading I think I found the correct way:

        protected override CompositionContainer CreateContainer()
            var exportFactoryProvider = new ExportFactoryProvider();
            var container = new CompositionContainer(AggregateCatalog, exportFactoryProvider);
            exportFactoryProvider.SourceProvider = container;
            return container;

The override of ConfigureContainer I came up with in my first post can be removed as it is not needed anymore.

Unfortunately while the catalog now contains the ExportFactoryProvider in the Providers property i receive another error while trying to use it.

When the class in which I defined the ViewModelFactory property loads I receive a NullPointerException at the following statement in the 'ExportFactoryInstantiationProvider.cs' file (part of Glenn's code) in the

protected override IEnumerable<Export> GetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition)

function. At

if (cbid == null || !cbid.RequiredTypeIdentity.StartsWith(PartCreatorContractPrefix))

The RequiredTypIdentity is null, so the exception thrown is a NullPointerException.

Am I doing anything wrong? The unit tests glenn created run fine.

Jan 22, 2012 at 10:02 PM

I have just compiled PRISM against the MEF 2.0 preview 5 source. All unit tests succeeded after doing so. ExportFactor<> now works like a charm. All the additional bootstrap configuration shown in this thread can be ignored when using ExportFactory<> from MEF 2. Please note that it's not recommended to run this configuration in production as MEF 2 isn't final yet.

Jan 23, 2012 at 7:17 PM


I am glad that you found a solution to your problem. Also, thanks for sharing your findings with the rest of the community as there might be other users that could find this useful.


Damian Cherubini

Jul 17, 2013 at 11:57 AM
In case anyone is still trying to use Glenn's ExportFactory lib: There is a fix for the exception Laurens describe. It's in the comments for the file on skydrive:

by Jean-Philippe Leconte:


I think I found a bug in the ExportFactoryInstantiationProvider.

At line 115 of Microsoft\ComponentModel\Composition\Hosting\ExportFactoryInstantiationProvider.cs, the line:

if (cbid == null || !cbid.RequiredTypeIdentity.StartsWith(PartCreatorContractPrefix))

should be:

if (cbid == null || cbid.RequiredTypeIdentity == null || !cbid.RequiredTypeIdentity.StartsWith(PartCreatorContractPrefix))

because the RequiredTypeIdentity (cbid.RequiredTypeIdentity) is null when there is no required type (Import by name only), thus calling StartsWith on a null string.


After changing that line of code I managed to get it working with Prism in .NET4.0