繁体   English   中英

MEF导出存储库 <T> :IRepository <T>

[英]MEF export of Repository<T> : IRepository<T>

我正在尝试使用MEF导出以下内容:

[Export(typeof(IRepository<>))]
public class Repository<T> : IRepository<T>
    where T : class
{

随着进口

[Import(typeof(IRepository<>))]
private IRepository<Contact> repository;

但是在撰写MEF时,我不断收到错误消息:

=========================================

构图保持不变。 由于以下错误,更改被拒绝:组合产生单个组合错误。 根本原因如下。 查看CompositionException.Errors属性以获取更多详细信息。

1)没有找到与约束'((exportDefinition.ContractName =“Interfaces.IRepository()”)&&(exportDefinition.Metadata.ContainsKey(“ExportTypeIdentity”)&&“Interfaces.IRepository()”匹配的有效导出.Equals(exportDefinition) .Metadata.get_Item(“ExportTypeIdentity”))))',无效导出可能已被拒绝。

导致:无法在部分'SoCLINQ2SQL.RepositoryTest'上设置导入'SoCLINQ2SQL.RepositoryTest.repository(ContractName =“Interfaces.IRepository()”)'。 元素:SoCLINQ2SQL.RepositoryTest.repository(ContractName =“Interfaces.IRepository()”) - > SoCLINQ2SQL.RepositoryTest

据我所知,根据Glenn Block关于这个主题的帖子 ,MEF不支持“开箱即用”的开放式通用类型。

显然,在MEF贡献项目中有它的支持。

我相信在这种情况下,您可以将导出保留为开放泛型类型,但在导入方面,您需要将导入更改为:

[Import(typeof(IRepository<Contact>))]
private IRepository<Contact> repository;

我遇到了类似的问题,它与程序集添加到AggregateCatalog的顺序有关。 下面的示例说明了Bootstrapper.ConfigureAggregateCatalog() “模块B”正在调用“模块C”中的服务,但“模块C”尚未添加到AggregatorCatalog中 我只需要改变顺序,它就解决了问题。

    protected override void ConfigureAggregateCatalog()
    {
        base.ConfigureAggregateCatalog();


 // Be aware of the order on which the assemblies are added to the AggregateCatalog.
 // It's important to add the assembly to the AggregateCatalog in the correct order, otherwise
 // you may get the error "No valid exports were found that match the constraint".
 // In the example below, if Module B invokes a method of Module C, module C must be
 // added to the AggregateCatalog prior to Module B. 
 // Please note the Bootstrapper assembly also needs to be added to the AggregateCatalog.
 // -------------------------------------------------------------------------------------- 
        this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
        this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleA).Assembly));
        this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleC).Assembly));
        this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleB).Assembly));

    }

仅供参考:即将推出的版本支持此功能。 预览下拉应该很快就会出现在我们的codeplex站点中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM