“无法加载一种或多种所请求的类型”在单独的项目中使用合同时

发布于 2024-10-01 07:57:37 字数 417 浏览 2 评论 0原文

可以说我有一个包含 4 个项目的解决方案:

  1. Namespace.Contracts - 包含 IContract 接口
  2. Namespace.Plugin.A - 包含类 A - IContract 实现。 A 类使用 MEF Namespace.App 导出
  3. - 从不同位置导入 IContract 类的应用程序,包括 Plugin.A 的程序集

我在项目 3 中编写时收到“无法加载一个或多个请求的类型”运行时异常 - MEF 抱怨 IContract 类型在 Namspace.App 中找不到...为什么它在这个命名空间而不是 Namespace.Contracts 中查找它? 如果我将 IContract 命名空间移动到 Namespace.App 项目,它就可以工作......我错过了什么?我应该如何组合零件?

Lets say I have a solution with 4 projects:

  1. Namespace.Contracts - contains IContract interface
  2. Namespace.Plugin.A - contains class A - IContract implementation. Class A is exported using MEF
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简单气质女生网名 2024-10-08 07:57:37

当程序集引用不存在的类型时,会出现此 ReflectionTypeLoadException 异常。这并不是 MEF 特有的;如果您加载麻烦的程序集并枚举其类型,如下所示,您将看到相同的异常:

var assembly = Assembly.Load("trouble.dll");
assembly.GetTypes();

在您的情况下,您在某个时刻编译了一个引用 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:

var assembly = Assembly.Load("trouble.dll");
assembly.GetTypes();

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 another Namespace.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.

拍不死你 2024-10-08 07:57:37

您确定在加载之前已将所有程序集添加到 MEF 容器中吗?请显示您用来编写的代码。
应该看起来像这样加载目录中的所有程序集:

var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new DirectoryCatalog(@".\"));

我正在使用此方法加载 MEF:

private bool Load(IEnumerable<string> addinDirectories)
{
    var catalog = new AggregateCatalog();
    catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetEntryAssembly()));
    foreach (string dir in addinDirectories)
    {
        DirectoryCatalog dc = new DirectoryCatalog(dir);
        catalog.Catalogs.Add(dc);
    }
    this.container = new CompositionContainer(catalog);

    CompositionBatch batch = new CompositionBatch();
    batch.AddPart(this);
    batch.AddExportedValue(container);

    this.container.Compose(batch);

    return this.success;
}

输入是我的 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:

var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new DirectoryCatalog(@".\"));

I'm using this method to load MEF:

private bool Load(IEnumerable<string> addinDirectories)
{
    var catalog = new AggregateCatalog();
    catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetEntryAssembly()));
    foreach (string dir in addinDirectories)
    {
        DirectoryCatalog dc = new DirectoryCatalog(dir);
        catalog.Catalogs.Add(dc);
    }
    this.container = new CompositionContainer(catalog);

    CompositionBatch batch = new CompositionBatch();
    batch.AddPart(this);
    batch.AddExportedValue(container);

    this.container.Compose(batch);

    return this.success;
}

The input is all the directories where my MEF assemblies are location, incl. the executing directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文