无法使用 MEF 获取对象
我有三个程序
- 集 AssemblyX 包含接口 IContext
- AssemblyY 包含实现 IContext 的类 Context
- AssemblyZ 是使用 Context 的客户端,应该将其注册到 MEF,可以从另一个类(在其他程序集中)使用 IContext
我的问题现在是在中注册 Context MEF,其他实例可以通过 IContext 获取它。
我已将属性 [Export(typeof(IContext))]
附加到 Context。但是,当我现在执行 this.Container.GetExportedValue
时,我得到一个 ImportCardinalityMismatchException。
Context 的构造函数定义如下
[ImportingConstructor]
public Context(IEventAggregator eventAggregator) { ... }
当我执行 this.Container.GetExportedValue
进行测试以解析 IEventAggregator 时,这是有效的。
有人可以帮助我,我做错了什么吗? - 我是否必须将 AssemblyX 和/或 AssemblyY 附加到某处?
谢谢。 托马斯
I have three Assemblies
- AssemblyX contains Interface IContext
- AssemblyY contains class Context which implements IContext
- AssemblyZ is the Client which uses Context and should register it to MEF, that IContext can be used from another Classes (in other Assemblies)
My Problem is now to register Context in MEF, that other instances can get it with IContext.
I have attached the Attribute [Export(typeof(IContext))]
to Context. But when I now do this.Container.GetExportedValue<IContext>()
I get a ImportCardinalityMismatchException.
The Constructor of Context is defined as follows
[ImportingConstructor]
public Context(IEventAggregator eventAggregator) { ... }
When I do this.Container.GetExportedValue<IEventAggregator>()
for Testing to resolve the IEventAggregator, this is working.
Can somebody help me, what I am doing wrong? - Does I have to attach somewhere the AssemblyX and/or AssemblyY?
Thanks.
Thomas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要解析 IContext(作为来自 assemblyY 的上下文),容器必须了解 AssemblyY(即,关于其中的所有可组合部件)。为此,您应该使用适当的目录。或者,如果 AssemblyY 是按需加载的,则应在使用 Context 之前加载它。
To resolve IContext(as Context from the assemblyY), the Container has to be aware of AssemblyY(that is, about all composable parts from it). In order to it, you should use the appropriate catalog. Or in case if AssemblyY is loaded on demand, it should be loaded before the Context is used.
MEF 正在使用目录中的所有三个程序集吗?
如果这不是问题,请查看我的博客文章 如何调试和诊断 MEF 故障。
Are all three of the assemblies in the catalog MEF is using?
If that's not the issue, have a look at my blog post on How to Debug and Diagnose MEF failures.