【问题标题】:Cannot getting object with MEF无法使用 MEF 获取对象
【发布时间】:2011-10-20 15:35:44
【问题描述】:

我有三个程序集

  • AssemblyX 包含接口 IContext
  • AssemblyY 包含实现 IContext 的类 Context
  • AssemblyZ 是使用 Context 的客户端,应将其注册到 MEF,IContext 可以从其他类(在其他程序集中)使用

我现在的问题是在 MEF 中注册 Context,其他实例可以通过 IContext 获取它。

我已将属性[Export(typeof(IContext))] 附加到上下文。但是当我现在执行 this.Container.GetExportedValue<IContext>() 时,我得到了 ImportCardinalityMismatchException。

Context的构造函数定义如下

    [ImportingConstructor]
    public Context(IEventAggregator eventAggregator) { ... }

当我执行 this.Container.GetExportedValue<IEventAggregator>() 进行测试以解析 IEventAggregator 时,这是有效的。

有人可以帮助我,我做错了什么吗? - 我是否必须在某个地方附加 AssemblyX 和/或 AssemblyY?

谢谢。 托马斯

【问题讨论】:

  • 检查:您是否导出了 IEventAggregator?另外,能否发布 ImportCardinalityMismatchException 的全文?
  • 消息是:找不到与约束匹配的有效导出 '((exportDefinition.ContractName == "Gemowin.Client.Interfaces.IGemowinClientContext") AndAlso (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity" ) AndAlso "Gemowin.Client.Interfaces.IGemowinClientContext".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))',无效的导出可能已被拒绝。是的,我已导出 IEventAggregator - this.Container.GetExportedValue() 正在工作。

标签: c# silverlight prism mef


【解决方案1】:

要解析 IContext(作为来自 assemblyY 的上下文),容器必须知道 AssemblyY(即,关于它的所有可组合部分)。为此,您应该使用适当的目录。或者,如果按需加载 AssemblyY,则应在使用 Context 之前加载它。

【讨论】:

    【解决方案2】:

    MEF 正在使用目录中的所有三个程序集吗?

    如果这不是问题,请查看我在 How to Debug and Diagnose MEF failures 上的博客文章。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-14
      • 2019-03-12
      • 1970-01-01
      相关资源
      最近更新 更多