【问题标题】:Visual Studio Extension MEF class not instantiated when installed via an MSI通过 MSI 安装时未实例化 Visual Studio 扩展 MEF 类
【发布时间】:2012-09-12 07:31:44
【问题描述】:

我正在编写一个 VS 2012 插件,其中包含一个 Package 以及一个 IWpfTextViewCreationListener MEF 扩展类。

它是使用创建 MSI 的 WiX 项目安装的,将所有文件放在 C:\Program Files\...[Application] 中;它创建 Packages 注册表项以指向包含 Package 实现的 DLL。

在实验性 Visual Studio 实例中调试插件时,一切运行正常。

运行 MSI 安装程序时,Package 代码运行良好,但未实例化 MEF 类。

注意:如果我使用 VSIX(我不用于 MSI 安装)安装软件包,一切也都可以正常工作。

MEF 类(与包在同一个程序集中):

[Export(typeof (IWpfTextViewCreationListener))]
[ContentType("text")]
[TextViewRole(PredefinedTextViewRoles.Document)]
internal sealed class HighlightAdornerFactory : IWpfTextViewCreationListener
{
    [Import]
    public IClassificationTypeRegistryService ClassificationRegistry = null;

    [Import]
    public IClassificationFormatMapService FormatMapService = null;

    [Export(typeof (AdornmentLayerDefinition))]
    [Name(HighlightAdornment.Name)] 
    [Order(After = PredefinedAdornmentLayers.Selection, Before = PredefinedAdornmentLayers.Text)]
    public AdornmentLayerDefinition editorAdornmentLayer = null;
 public void TextViewCreated(IWpfTextView textView)
    { /** ... **/ }
}

我使用VisualMEFX 来检查包含它的DLL。它报告没有导出匹配IClassificationTypeRegistryService。这并没有解释为什么它在与 VSIX 一起安装或通过 IDE 调试时工作。但了解问题可能有助于解决问题。

  [Export] MySolution.HighlightAdornerFactory (ContractName="Microsoft.VisualStudio.Text.Editor.IWpfTextViewCreationListener")
  [Export] MySolution.Adornment.HighlightAdornerFactory.editorAdornmentLayer (ContractName="Microsoft.VisualStudio.Text.Editor.AdornmentLayerDefinition")
  [Import] MySolution.Adornment.HighlightAdornerFactory.ClassificationRegistry (ContractName="Microsoft.VisualStudio.Text.Classification.IClassificationTypeRegistryService")
    [Exception] System.ComponentModel.Composition.ImportCardinalityMismatchException: No exports were found that match the constraint: 
    ContractName    Microsoft.VisualStudio.Text.Classification.IClassificationTypeRegistryService
    RequiredTypeIdentity    Microsoft.VisualStudio.Text.Classification.IClassificationTypeRegistryService
   at System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(ImportDefinition definition, AtomicComposition atomicComposition)
   at System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(ImportDefinition definition)
   at Microsoft.ComponentModel.Composition.Diagnostics.CompositionInfo.AnalyzeImportDefinition(ExportProvider host, IEnumerable`1 availableParts, ImportDefinition id) in C:\Temp\MEF_Beta_2\Samples\CompositionDiagnostics\Microsoft.ComponentModel.Composition.Diagnostics\CompositionInfo.cs:line 157
  [Import] MySolution.HighlightAdornerFactory.FormatMapService (ContractName="Microsoft.VisualStudio.Text.Classification.IClassificationFormatMapService")
    [Exception] System.ComponentModel.Composition.ImportCardinalityMismatchException: No exports were found that match the constraint: 
    ContractName    Microsoft.VisualStudio.Text.Classification.IClassificationFormatMapService
    RequiredTypeIdentity    Microsoft.VisualStudio.Text.Classification.IClassificationFormatMapService
   at System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(ImportDefinition definition, AtomicComposition atomicComposition)
   at System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(ImportDefinition definition)
   at Microsoft.ComponentModel.Composition.Diagnostics.CompositionInfo.AnalyzeImportDefinition(ExportProvider host, IEnumerable`1 availableParts, ImportDefinition id) in C:\Temp\MEF_Beta_2\Samples\CompositionDiagnostics\Microsoft.ComponentModel.Composition.Diagnostics\CompositionInfo.cs:line 157

我尝试将程序集中的所有引用库添加到 MSI,但这没有帮助。我没有看到任何异常被抛出,IWpfTextViewCreationListener 类根本没有被加载。

【问题讨论】:

    标签: visual-studio wix mef visual-studio-2012 visual-studio-extensions


    【解决方案1】:

    您看到的行为是设计使然;只有在某处的 .vsixmanifest 中列出程序集时,它们才会包含在 MEF 组合中。当您通过 MSI 添加 Package 注册表项时,这不会将您的程序集添加到 MEF 组合中。您什么时候在实验性配置单元中进行测试或直接安装扩展程序时,您正在按照预期将您的程序集包含到 MEF 组合中。

    按照我的推荐顺序,您有几种方法可以解决此问题:

    1. 不要使用 MSI。如果可能的话,我强烈鼓励这样做。 VSIX 非常强大,为用户提供了许多优势:它们更易于安装和卸载,并且与扩展管理器和 Visual Studio 库集成。除非您能证明 VSIX 不适合您的方案,否则请使用它们。

    2. 使用 WiX 3.6,它有一个 VsixPackage 元素,它将为您安装 VSIX。文档可通过here 获取。

    3. 让您的 MSI 将 VSIX 的内容安装到 [Program Files]\Microsoft Visual Studio 11.0\Common7\IDE\Extensions\[Your Product Name],然后运行 ​​devenv /setup 作为自定义操作的一部分.这非常棘手,需要大量手动 WiX 创作。选项#2 的存在是有原因的——WiX 人实际上知道他们在做什么。 ;-)

    【讨论】:

    • 谢谢杰森。我昨晚找到了 wix VsixPackage 元素,我会试一试。感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多