【问题标题】:MEF component with [Export] in VS extension - No exports found that match the constraintVS 扩展中具有 [Export] 的 MEF 组件 - 未找到与约束匹配的导出
【发布时间】:2017-01-29 16:59:56
【问题描述】:

在 Visual Studio 扩展中,我尝试导出 MEF 组件并稍后找到它。

我有这门课:

[Export(typeof(IBoilerplateSettings))]
public class BoilerplateSettings : IBoilerplateSettings
{
    ...

并且此代码在来自 OleMenuCommand 的回调中解析。从this pull request 获取示例。

var componentModel = (IComponentModel)this.package.GetService(typeof(SComponentModel));
var settings = componentModel.DefaultExportProvider.GetExportedValue<IBoilerplateSettings>();

但是每次都会抛出这个错误:

No exports were found that match the constraint: 
    ContractName    UnitTestBoilerplate.IBoilerplateSettings
    RequiredTypeIdentity    UnitTestBoilerplate.IBoilerplateSettings

两者都在同一个程序集中。我已经在System.CompositionSystem.ComponentModel.Composition 中尝试了[Export] 属性,但都不起作用。

我正在查看the docs for MEF in VS,但听起来添加属性应该可以工作。我也尝试过清除 ComponentModel 缓存,但没有奏效。我在这里错过了什么?

【问题讨论】:

    标签: c# visual-studio mef visual-studio-extensions


    【解决方案1】:

    一般要检查两件事:

    1. 确保您的项目在 .vsixmanifest 中被提及为 MEF 组件。如果不是,VS 将不会查看您的程序集。
    2. 如果您使用的是 Visual Studio 2015 或更高版本,则在 ComponentModelCache 文件夹中有一个 .err 文件,其中列出了创建 MEF 组合时出现的错误。确保你关心的东西不在里面。 (里面有垃圾是正常的……我们并不总是最擅长在干净的状态下运送垃圾。)

    如果这不起作用,通常会有一种激进的方法,即在 Visual Studio 运行时中断所有异常,并查看是否可以找到与扩展相关的任何内容。您可能会发现一些其他加载异常,或其他一些根本原因。不幸的是,这是一种非常分散的方法。

    【讨论】:

    • 我尝试在资产下添加&lt;Asset Type="Microsoft.VisualStudio.MefComponent" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%|" /&gt;,但这似乎没有帮助。 .err 文件为 0 字节。
    • 并且只是检查:如果您有多个项目被放在同一个 .vsix 中,那么您在其中列出了正确的项目吗?否则,是的,看起来不错......
    • .vsix 中只有一个项目。
    • 是否清理了 Visual Studio 组件模型缓存? %AppData%\..\Local\Microsoft\VisualStudio\11.0\ComponentModelCache
    • 再次尝试这个,在添加 MefComponent 资产后,事情似乎在这个复飞中工作。
    【解决方案2】:

    谢谢杰森·马林诺夫斯基。 这很重要。如果你想在 vsix 中使用 MEF。 需要注意vsixmainfest中的资源是否有对应的MefComponent。

    【讨论】:

      猜你喜欢
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-17
      • 1970-01-01
      • 2022-10-06
      • 2014-07-13
      • 1970-01-01
      相关资源
      最近更新 更多