【问题标题】:MEF export of Repository<T> : IRepository<T>存储库<T> 的 MEF 导出:IRepository<T>
【发布时间】:2010-06-23 07:51:51
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: c# generics mef


    【解决方案1】:

    据我所知,根据 Glenn Block's post 关于该主题的说法,MEF 不支持“开箱即用”的开放泛型类型。

    MEF contrib project 显然支持它。

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

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

    【讨论】:

      【解决方案2】:

      我有一个类似的问题,它与将程序集添加到 AggregateCatalog 的顺序有关。下面的示例说明了 Bootstrapper.ConfigureAggregateCatalog()“Module B” 正在从 “Module C” 调用服务,但 “Module 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));
      
          }
      

      【讨论】:

        【解决方案3】:

        仅供参考:即将推出的版本支持此功能。预览会很快出现在我们的 codeplex 网站上。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-10-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-22
          • 1970-01-01
          • 2014-09-21
          相关资源
          最近更新 更多