通过MSI安装时,Visual Studio扩展MEF类未实例化

时间:2012-09-19 23:00:20

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

我正在编写一个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类根本没有被加载。

1 个答案:

答案 0 :(得分:2)

您所看到的行为是设计的;只有当它们列在某个.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 \ [您的产品名称]中,然后将devenv / setup作为自定义操作的一部分运行。这非常棘手,需要大量的手动WiX创作。选项#2存在是有原因的 - WiX人员实际上知道他们在做什么。 ; - )

相关问题