带有Visual Studio 2015扩展的MEF导入异常

时间:2015-09-12 12:35:18

标签: visual-studio visual-studio-2015 mef visual-studio-extensions vsix

希望有人可以就我在使用MEF和我的VS2015扩展时遇到的问题向我提供一些指导。我试图扩展VS的IntelliSense功能,并以本教程为基础:https://msdn.microsoft.com/en-us/library/ee372314.aspx

这是我的ICompletionSourceProvider:

[Export(typeof(ICompletionSourceProvider))]
[Name("Test Statement Completion Provider")]
[Order(Before = "default")]
[ContentType("text")]
internal class MyCompletionSourceProvider : ICompletionSourceProvider
{
    [Import]
    internal ITextStructureNavigatorSelectorService NavigatorService { get; set; }

    [Import]
    internal IServiceProvider ServiceProvider { get; set; }

    public ICompletionSource TryCreateCompletionSource(ITextBuffer textBuffer)
    {
        return new MyCompletionSource(this, textBuffer);
    }
}

这里奇怪的是,这在我的机器上可以正常工作(这个代码最初是在这个代码上开发的),但是在我尝试过的所有其他机器上都无法工作(在Debug和Release中)。 TryCreateCompletionSource中的断点永远不会被命中。我怀疑这个MEF组件从未被导出过,所以我在MSDN上下载了mefx工具,并在我生成的DLL上执行它并收到以下内容:

[Primary Rejection]
[Export] MyVSIX.Source.MyCompletionSourceProvider             (ContractName="Microsoft.VisualStudio.Language.Intellisense.ICompletionSourceProvider")
[Import] MyVSIX.Source.MyCompletionSourceProvider.NavigatorService (ContractName="Microsoft.VisualStudio.Text.Operations.ITextStructureNavigatorSelectorService")
[Exception]   System.ComponentModel.Composition.ImportCardinalityMismatchException: No exports   were found that match the constraint:
ContractName      Microsoft.VisualStudio.Text.Operations.ITextStructureNavigatorSelectorService    
RequiredTypeIdentity      Microsoft.VisualStudio.Text.Operations.ITextStructureNavigatorSelectorService
at System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(ImportDefinition definition, AtomicComposition atomicComposition)
at Microsoft.ComponentModel.Composition.Diagnostics.CompositionInfo.AnalyzeImportDefinition(ExportProvider host, IEnumerable`1 availableParts, ImportDefinition id)    
[Import] MyVSIX.Source.MyCompletionSourceProvider.ServiceProvider (ContractName="System.IServiceProvider")
[Exception] System.ComponentModel.Composition.ImportCardinalityMismatchException: No exports were found that match the constraint: 
ContractName    System.IServiceProvider
RequiredTypeIdentity    System.IServiceProvider
at System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(ImportDefinition definition, AtomicComposition atomicComposition)
at Microsoft.ComponentModel.Composition.Diagnostics.CompositionInfo.AnalyzeImportDefinition(ExportProvider host, IEnumerable`1 availableParts, ImportDefinition id)

如果我删除[Import]语句,这些都会被解决,并且事情会再次开始。但是从教程中看,这似乎应该可行。这也无法解释为什么它在我的机器上100%的工作时间,并且永远不能在任何其他机器上工作。

这是我的.vsixmanifest文件:

<?xml version="1.0" encoding="utf-8"?>
  <PackageManifest Version="2.0.0"  xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011"  xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
    <Metadata>
    <Identity Id="MyVSIX.Company.39048217-955a-4402-84e9-8a24f0730e72" Version="1.0" Language="en-US" Publisher="MyVSIX" />
    <DisplayName>MyVSIX</DisplayName>
    <Description xml:space="preserve"></Description>
    <Icon>Resources\MyVSIX.ico</Icon>
  </Metadata>
  <Installation>
  <InstallationTarget Id="Microsoft.VisualStudio.Community" Version="[14.0]" />
  </Installation>
  <Dependencies>
    <Dependency Id="Microsoft.Framework.NDP" DisplayName="Microsoft .NET Framework" d:Source="Manual" Version="[4.5,)" />
    <Dependency Id="Microsoft.VisualStudio.MPF.14.0" DisplayName="Visual Studio MPF 14.0" d:Source="Installed" Version="[14.0]" />
  </Dependencies>
  <Assets>
    <Asset Type="Microsoft.VisualStudio.VsPackage" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%;PkgdefProjectOutputGroup|" />
    <Asset Type="Microsoft.VisualStudio.MefComponent" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%|" />
    <Asset Type="Microsoft.VisualStudio.ItemTemplate" Path="Output\ItemTemplates" />
    <Asset Type="Microsoft.VisualStudio.ProjectTemplate" Path="Output\ProjectTemplates" />
  </Assets>
</PackageManifest>

1 个答案:

答案 0 :(得分:2)

弄清楚它为什么不在其他机器上工作,但我仍然无法理解为什么它在我的机器上工作。我改变了这个:

[Import(typeof(SVsServiceProvider)]
internal IServiceProvider ServiceProvider { get; set; }

到此:

lines = read.table(filename, sep="\n")

希望这可以帮助将来的某个人 - 我花了两天时间搞清楚这一点!