10

在我的应用程序的根目录中,我有一个 AggregateCatalog 和一个 CompositionContainer,如下所示:

AggregateCatalog aggregateCatalog = new AggregateCatalog();
CompositionContainer compositionContainer = new CompositionContainer(aggregateCatalog);

我的应用程序加载了包含多个导出的模块,如下图所示。我想使用 CompositionScopeDefinition 来确定图中圈出的导出范围。

模块导出图

有关类定义,请参见此处。

// 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>();

但是,这会导致以下异常:

System.ComponentModel.Composition.ImportCardinalityMismatchException 发生消息=未找到与约束匹配的导出:ContractName MyNamespace.RequestListener RequiredTypeIdentity MyNamespace.RequestListener InnerException:

如何使用 CompositionScopeDefinition 将我的范围导出添加到现有的 AggregateCatalog 并使用我现有的 CompositionContainer 对其进行初始化?

更新

似乎是使用 AggregateCatalog 的问题。如果我将 CompositionScopeDefinition 直接添加到 CompositionContainer 中,一切正常,但这会阻止我将其他目录添加到 CompositionContainer。

4

1 回答 1

2

我与在 CodePlex 上从事 MEF 工作的人交谈过。这基本上是他们的答案:

// Handy extension methods for dealing with CompositionScopeDefinition (Not relevant to this answer but useful).
public static class ComposablePartCatalogExtensions
{
    public static CompositionScopeDefinition AsScope(this ComposablePartCatalog catalog, params CompositionScopeDefinition[] children)
    {
        return new CompositionScopeDefinition(catalog, children);
    }

    public static CompositionScopeDefinition AsScopeWithPublicSurface<T>(this ComposablePartCatalog catalog, params CompositionScopeDefinition[] children)
    {
        IEnumerable<ExportDefinition> definitions = catalog.Parts.SelectMany((p) => p.ExportDefinitions.Where((e) => e.ContractName == AttributedModelServices.GetContractName(typeof(T))));
        return new CompositionScopeDefinition(catalog, children, definitions);
    }
}

AggregateCatalog aggregateCatalog = new AggregateCatalog();
AggregateCatalog childAggregateCatalog = new AggregateCatalog();
CompositionScopeDefinition compositionScopeDefinition = aggregateCatalog.AsScope(childAggregateCatalog.AsScope());
CompositionContainer compositionContainer = new CompositionContainer(compositionScopeDefinition);

TypeCatalog globalParts = new TypeCatalog(typeof(RequestListener));
TypeCatalog scopedParts = new TypeCatalog(typeof(RequestHandler), typeof(DataAccessLayer), typeof(Logger), typeof(DatabaseConnection));

aggregateCatalog.Catalogs.Add(globalParts);
childAggregateCatalog.Catalogs.Add(scopedParts);

RequestListener requestListener = compositionContainer.GetExportedValue<RequestListener>();

本质上,您不能将 CompositionScopeDefinition 放在 AggregateCatalog 中。因此,您可以反转关系并在根级别拥有一个 CompositionScopeDefinition,并为您尝试表示的每个范围级别拥有多个 AggregateCatalog。这似乎工作得很好。您还可以获得拥有单个 CompositionContainer 的额外好处。

于 2013-06-24T12:03:34.020 回答