“无法加载一种或多种所请求的类型”在单独的项目中使用合同时
可以说我有一个包含 4 个项目的解决方案:
- Namespace.Contracts - 包含 IContract 接口
- Namespace.Plugin.A - 包含类 A - IContract 实现。 A 类使用 MEF Namespace.App 导出
- - 从不同位置导入 IContract 类的应用程序,包括 Plugin.A 的程序集
我在项目 3 中编写时收到“无法加载一个或多个请求的类型”运行时异常 - MEF 抱怨 IContract 类型在 Namspace.App 中找不到...为什么它在这个命名空间而不是 Namespace.Contracts 中查找它? 如果我将 IContract 命名空间移动到 Namespace.App 项目,它就可以工作......我错过了什么?我应该如何组合零件?
Lets say I have a solution with 4 projects:
- Namespace.Contracts - contains IContract interface
- Namespace.Plugin.A - contains class A - IContract implementation. Class A is exported using MEF
- Namespace.App - application that imports IContract classes from different locations including Plugin.A's assembly
I receive "Unable to load one or more of the requested types" runtime exception while composing in project 3 - MEF complains that IContract type cannot be found in Namspace.App ... why does it look for it in this namespace and not Namespace.Contracts?
If I move IContract namespace to Namespace.App project it works... what do I miss? How should I compose the parts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当程序集引用不存在的类型时,会出现此
ReflectionTypeLoadException
异常。这并不是 MEF 特有的;如果您加载麻烦的程序集并枚举其类型,如下所示,您将看到相同的异常:在您的情况下,您在某个时刻编译了一个引用
Namespace.App.IContract
类型的插件程序集。然后,将此程序集复制到应用程序 bin 文件夹,MEF 可以在其中拾取它。后来,您将
IContract
接口移至另一个Namespace.Contracts
程序集 - 但旧的插件程序集仍需要在其先前位置中使用该类型。您需要重新编译插件程序集并重新部署它。This
ReflectionTypeLoadException
is what you get when your assembly references a type that does not exist. This is not specific to MEF; you'll see the same exception if you load the troublesome assembly and enumerate its types like this:In your case, you have at some point compiled a plugin assembly which references the type
Namespace.App.IContract
. You then copied this assembly to the application bin folder where it could be picked up by MEF.Later, you moved the
IContract
interface to anotherNamespace.Contracts
assembly - but the old plugin assembly is still expecting that type in its previous location. You need to recompile the plugin assembly and redeploy it.您确定在加载之前已将所有程序集添加到 MEF 容器中吗?请显示您用来编写的代码。
应该看起来像这样加载目录中的所有程序集:
我正在使用此方法加载 MEF:
输入是我的 MEF 程序集所在的所有目录,包括。执行目录。
Are you sure that you have added all the assemblies to the MEF container before loading? Please display the code that you use to compose.
Should look something like this to load all assemblies in a directory:
I'm using this method to load MEF:
The input is all the directories where my MEF assemblies are location, incl. the executing directory.