At the root of my application I have an AggregateCatalog and a CompositionContainer like so:
AggregateCatalog aggregateCatalog = new AggregateCatalog();
CompositionContainer compositionContainer = new CompositionContainer(aggregateCatalog);
My application loads up modules which contain several exports as shown in the diagram below. I want to use CompositionScopeDefinition to scope the exports circled in the diagram.
See here for the class definitions.
// Create CompositionScopeDefinition.
TypeCatalog globalParts = new TypeCatalog(typeof(RequestListener));
TypeCatalog scopedParts = new TypeCatalog(typeof(RequestHandler), typeof(DataAccessLayer), typeof(Logger), typeof(DatabaseConnection));
CompositionScopeDefinition compositionScopeDefinition = new CompositionScopeDefinition(
globalParts,
new[] { new CompositionScopeDefinition(scopedParts, null) });
// Register CompositionScopeDefinition.
aggregateCatalog.Catalogs.Add(compositionScopeDefinition);
// Create an instance of RequestListener.
RequestListener requestListener = compositionContainer.GetExportedValue<RequestListener>();
However, this causes the following exception:
System.ComponentModel.Composition.ImportCardinalityMismatchException occurred Message=No exports were found that match the constraint: ContractName MyNamespace.RequestListener RequiredTypeIdentity MyNamespace.RequestListener InnerException:
How can add my scoped exports using CompositionScopeDefinition to an existing AggregateCatalog and initialise them using my existing CompositionContainer?
Update
It seems that the problem using an AggregateCatalog. If I add the CompositionScopeDefinition to the CompositionContainer directly everything works but this stops me from adding other catalogs to the CompositionContainer.