MEF 在其他装配体中找不到零件
当谈到使用 MEF 时,我缺少一些基本的东西。我使用示例和一个简单的控制台应用程序让它工作,其中所有内容都在同一个程序集中。然后我将一些导入和导出放入一个包含各种实体的单独项目中。我想在 MS 测试中使用这些实体,但组合从未真正完成。当我将组合内容移动到相关实体的构造函数中时,它可以工作,但这显然是错误的。 GetExecutingAssembly 是否只“看到”测试过程?我缺少什么重新容器?我尝试将容器放入“使用”测试中,但没有成功。 MEF 文档仍然非常少,我找不到使用来自不同项目的实体的应用程序(或 MS Test)的简单示例......
I am missing something basic when it comes to using MEF. I got it working using samples and a simple console app where everything is in the same assembly. Then I put some imports and exports in a separate project which contains various entities. I want to use these entities in an MS Test, but the composition is never actually done. When I move the composition stuff into the constructor of an entity in question it works, but that's obviously wrong. Does GetExecutingAssembly only "see" the test process? What am I missing re containers? I tried putting the container in a Using in the test without luck. The MEF docs are still very scant and I can't find a simple example of an application (or MS Test) which uses entities from a different project...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 .NET 中,每个 exe 或 DLL 文件称为程序集1。因此,当您基于“执行程序集”构建目录并在应用程序入口点使用它时,您只包含 exe 项目中定义的部分。您不会获得 DLL 中定义的任何部分。
尝试替换这个:
通过这个:
编辑:我刚刚发现有一个更简单的解决方案:(
1)实际上,一个程序集可以由多个文件组成,但是这个很少使用。该术语也用于并排 COM。
In .NET, each exe or DLL file is called an assembly1. So when you build a catalog based on the "executing assembly" and use it at the application entry point, then you only include parts which are defined within the exe project. You don't get any parts defined in the DLLs.
Try replacing this:
by this:
edit: I just discovered that there is a simpler solution:
(1) Actually, an assembly can consist of multiple files but this is rarely used. The term is also used for side-by-side COM.
是的。您需要确保在合成之前将程序集(具有导入和导出的程序集)添加到目录中。这样,它就可以找到合适的零件。
GetExecutingAssembly 的作用正如其所言——它获取当前正在执行的程序集,这意味着编写了特定代码的程序集。就您而言,这是测试程序集,而不是您的“库”项目。
让您的测试显式地将库项目添加到目录中,它很可能会按您的预期工作。
Yes. You need to make sure to add your assembly (the one with imports and exports) to the catalog prior to composition. This way, it can find the appropriate parts.
GetExecutingAssembly does exactly what it says - it gets the assembly that's currently executing, which means the one that has that specific code written. In your case, this is the test assembly, not your "library" project.
Have your test add the library project to the catalog explicitly, and it will most likely work as you expect.