CompositionHost.Initialize() can not execute twice.

Posted by Khoa on Stack Overflow See other posts from Stack Overflow or by Khoa
Published on 2011-06-28T05:46:26Z Indexed on 2011/06/28 8:22 UTC
Read the original article Hit count: 232

Filed under:

I am currently try to integrate MEF and PRISM to work with each other. Everything is working fine so far. Now i would like to use MEF runtime module discovery (DeploymentCatalog) which will be used to download XAPs from server directory and then plug it into one of the Region inside my MAIN UI.

I am using UnityBootStrapper and inside this class i also integrated MEF container. This sample application is based on Glenn Block (http://codebetter.com/glennblock/2010/01/03/mef-and-prism-exploration-mef-module-loading/).

The following code is used to initialize CompositionContainer inside my Bootstrapper:

     // This is the host catalog which contains all parts of running assembly.
        var catalog = GetHostCatalog();

        // Create MEF container which initial catalog
        var container = new CompositionContainer(catalog);


        // here we explicitly map a part to make it available on imports elsewhere, using
        // Unity to resolve the export so dependencies are resolved
        // We do this because region manager is third-party ... therefore, we need to 
        // export explicitly because the implementation doesn't have its own [export] tag
        container.ComposeExportedValue<IRegionManager>(Container.Resolve<IRegionManager>());
        container.ComposeExportedValue<IEventAggregator>(Container.Resolve<IEventAggregator>());

        // Obtain CatalogService as a singleton
        // All dynamic modules will use this service to add its parts.
        Container.RegisterInstance<ICatalogService>(new CatalogService(catalog));


        // Initialize the container
        CompositionHost.Initialize(container);

Now i have another class called DeploymentCatalogService which is used to download the XAP from server. The current problem i am facing is that inside DeploymentCatalogService Initialize method, CompositionHost container is try to initialize its container with aggregateCatalog again.

_aggregateCatalog = new AggregateCatalog();
        _aggregateCatalog.Catalogs.Add(new DeploymentCatalog());
        CompositionHost.Initialize(_aggregateCatalog);

This causes an Exception which stated that The container has already been initialized. Is there a way to use the existing container and update it with the new aggregateCatalog?

Hope this is not too confusing. Please be nice i am still new to MEF.

Cheers,

© Stack Overflow or respective owner

Related posts about MEF