我目前正在尝试整合MEF和PRISM以便彼此合作。到目前为止一切正常。现在我想使用MEF运行时模块发现(DeploymentCatalog),它将用于从服务器目录下载XAP,然后将其插入我MAIN UI中的一个Region。
我正在使用UnityBootStrapper,在这个类中我也集成了MEF容器。此示例应用程序基于Glenn Block(http://codebetter.com/glennblock/2010/01/03/mef-and-prism-exploration-mef-module-loading/)。
以下代码用于初始化Bootstrapper中的CompositionContainer:
// 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);
现在我有另一个名为DeploymentCatalogService的类,用于从服务器下载XAP。我面临的当前问题是在DeploymentCatalogService Initialize方法内部,CompositionHost容器尝试再次使用aggregateCatalog初始化其容器。
_aggregateCatalog = new AggregateCatalog();
_aggregateCatalog.Catalogs.Add(new DeploymentCatalog());
CompositionHost.Initialize(_aggregateCatalog);
这会导致异常,表明容器已经初始化。有没有办法使用现有容器并使用新的aggregateCatalog更新它?
希望这不会太混乱。请你好,我还是MEF的新手。
干杯,
答案 0 :(得分:0)
您只能初始化容器一次。在您的情况下,您应该使用AggregateCatalog创建容器并存储对该目录的引用。然后,您可以将DeploymentCatalog添加到该AggregateCatalog。