0

我正在编写一个VS 2012插件,其中包含一个Package以及一个IWpfTextViewCreationListenerMEF扩展类。Visual Studio扩展MEF类未通过MSI安装时实例化

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

在实验性Visual Studio实例中调试插件时,所有内容都正常运行。

运行MSI安装程序时,程序包代码运行正常,但未实例化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类完全没有加载。

回答

2

你看到的行为是通过设计;只有在某个地方的.vsixmanifest中列出了MEF组合时,它们才会包含在MEF组合中。当您通过MSI添加包注册表项时,这不会将您的程序集添加到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人真的知道他们在做什么。 ;-)

+0

谢谢杰森。我昨天晚上发现了wix VsixPackage元素,并且会给你一个镜头。我很欣赏这种回应。 –

相关问题