CompositionHost.Initialize() can not execute twice
我目前正在尝试将MEF和Prism集成起来,以便彼此协作。到目前为止一切正常。现在,我想使用MEF运行时模块发现(DeploymentCatalog),它将用于从服务器目录下载XAP,然后将其插入我的主UI中的一个区域。
我使用的是Unitybootstrapper,在这个类中,我还集成了MEF容器。此示例应用程序基于Glenn块(http://codebetter.com/glenn block/2010/01/03/mef和prism exploration mef module loading/)。
以下代码用于在引导程序中初始化compositioncontainer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | // 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初始化方法中,compositionHost容器尝试再次使用aggregateCatalog初始化其容器。
1 2 3 | _aggregateCatalog = new AggregateCatalog(); _aggregateCatalog.Catalogs.Add(new DeploymentCatalog()); CompositionHost.Initialize(_aggregateCatalog); |
这将导致一个异常,说明容器已初始化。是否有方法使用现有容器并使用新的AggregateCatalog更新它?
希望这不会太令人困惑。拜托,我对我还是个新手。
干杯,
只能初始化一次容器。在您的例子中,您应该使用AggregateCatalog创建容器,并存储对该目录的引用。然后,您可以将DeploymentCatalog添加到AggregateCatalog中。