2

I'm new to Visual Studio extensions and tried some samples from GitHub together with some helpful advices from here with some success. I'm trying to create a ToolWindow that processes the structure of the currently open solution, which would be technically no problem with the Microsoft.VisualStudio.LanguageServices namespace. The ToolWindow per se works, as long as I don't use anything from that namespace - in particular the following called function initializes the solution information in the constructor of my window:

private void ReloadSolutionInformation()
{
    var componentModel = (IComponentModel)SmartOptionsEditorPackage.GetGlobalService(typeof(SComponentModel));
    VisualStudioWorkspace workspace = componentModel.GetService<VisualStudioWorkspace>();
    Solution solution = workspace.CurrentSolution;

    this.Solutions.Clear(); // some collection of SolutionNode instances

    // create and add custom tree node with solution information
    var solutionNode = new SolutionNode(solution.FilePath);
    this.Solutions.Add(solutionNode);

    // recurse through projects etc.
    PopulateSolutionWithProjects(solutionNode, solution);
}

When I run the extension to debug it in an experimental instance of Visual Studio 2017, I get the following exception (original is german, my translation might not be 100% accurate) in the constructor, where the above method is invoked:

The File or Assembly "Microsoft.VisualStudio.LanguageServices, Version=2.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" or one of its dependencies could not be found. The system cannot find the file.

StackTrace: at MyAssembly.MyToolWindow.ReloadSolutionInformation() at MyAssembly.MyToolWindow..ctor() at MyAssembly.MyToolWindowPane..ctor()

FileName: Microsoft.VisualStudio.LanguageServices, Version=2.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

Furthermore, I noticed the following warning after build:

CS1762: A reference was created to embedded interop assembly 'Microsoft.VisualStudio.Shell.Interop.10.0, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' because of an indirect reference to that assembly created by assembly 'Microsoft.VisualStudio.LanguageServices, Version=2.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Consider changing the 'Embed Interop Types' property on either assembly.

With build output verbosity set to diagnostic, I only found the above warning without further information. Examining the build output directory, I see that all referenced DLLs are contained, but all Microsoft.VisualStudio.* DLLs are missing in the VSIX, except for Microsoft.VisualStudio.Shell.Interop.15.3.DesignTime.dll. As far as I understand the interop assemblies, I'd think they are supplied by VisualStudio, so I don't have to embed them, do I?

The reference property Embed Interop Types is set to false for each reference. I tried to change it according to some advices I found, but that didn't work. I'm targeting .Net 4.6.2.

After far too many evenings of frustrated trial-and-especially-error, I still don't know, how I can fix this. This includes reinstalling NuGet packages, restarting everything, resetting the experimental instance and step-by-step removing and re-adding references as both local DLL and NuGetPackage. In a former edit I posted a shortened project file content and I had to remove it now, due to input character limit. Instead, here is the output of fusionlog:

=== Zustandsinformationen vor Bindung ===
LOG: DisplayName = Microsoft.VisualStudio.LanguageServices, Version=2.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
 (Fully-specified)
LOG: Appbase = file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/
LOG: Ursprünglicher PrivatePath = NULL
Aufruf von Assembly : MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: Diese Bindung startet im LoadFrom-Load-Kontext.
WRN: Das systemeigene Image wird nicht im LoadFrom-Kontext durchsucht. Das systemeigene Image wird nur im Standard-Load-Kontext durchsucht, z. B. Assembly.Load().
LOG: Die Anwendungskonfigurationsdatei wird verwendet: C:\Users\me\AppData\Local\Microsoft\VisualStudio\15.0_15d7a44dExp\devenv.exe.config
LOG: Die Hostkonfigurationsdatei wird verwendet: 
LOG: Die Computerkonfigurationsdatei von C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config wird verwendet.
LOG: Verweis nach der Richtlinie: Microsoft.VisualStudio.LanguageServices, Version=2.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/Microsoft.VisualStudio.LanguageServices.DLL.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/Microsoft.VisualStudio.LanguageServices/Microsoft.VisualStudio.LanguageServices.DLL.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/PublicAssemblies/Microsoft.VisualStudio.LanguageServices.DLL.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/PublicAssemblies/Microsoft.VisualStudio.LanguageServices/Microsoft.VisualStudio.LanguageServices.DLL.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/PrivateAssemblies/Microsoft.VisualStudio.LanguageServices.DLL.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/PrivateAssemblies/Microsoft.VisualStudio.LanguageServices/Microsoft.VisualStudio.LanguageServices.DLL.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/CommonExtensions/Microsoft/TemplateProviders/Microsoft.VisualStudio.LanguageServices.DLL.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/CommonExtensions/Microsoft/TemplateProviders/Microsoft.VisualStudio.LanguageServices/Microsoft.VisualStudio.LanguageServices.DLL.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/CommonExtensions/Platform/Debugger/Microsoft.VisualStudio.LanguageServices.DLL.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/CommonExtensions/Platform/Debugger/Microsoft.VisualStudio.LanguageServices/Microsoft.VisualStudio.LanguageServices.DLL.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/CommonExtensions/Platform/DiagnosticsHub/Microsoft.VisualStudio.LanguageServices.DLL.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/CommonExtensions/Platform/DiagnosticsHub/Microsoft.VisualStudio.LanguageServices/Microsoft.VisualStudio.LanguageServices.DLL.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/PrivateAssemblies/DataCollectors/Microsoft.VisualStudio.LanguageServices.DLL.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/PrivateAssemblies/DataCollectors/Microsoft.VisualStudio.LanguageServices/Microsoft.VisualStudio.LanguageServices.DLL.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/PrivateAssemblies/DataCollectors/x86/Microsoft.VisualStudio.LanguageServices.DLL.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/PrivateAssemblies/DataCollectors/x86/Microsoft.VisualStudio.LanguageServices/Microsoft.VisualStudio.LanguageServices.DLL.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/Microsoft.VisualStudio.LanguageServices.EXE.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/Microsoft.VisualStudio.LanguageServices/Microsoft.VisualStudio.LanguageServices.EXE.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/PublicAssemblies/Microsoft.VisualStudio.LanguageServices.EXE.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/PublicAssemblies/Microsoft.VisualStudio.LanguageServices/Microsoft.VisualStudio.LanguageServices.EXE.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/PrivateAssemblies/Microsoft.VisualStudio.LanguageServices.EXE.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/PrivateAssemblies/Microsoft.VisualStudio.LanguageServices/Microsoft.VisualStudio.LanguageServices.EXE.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/CommonExtensions/Microsoft/TemplateProviders/Microsoft.VisualStudio.LanguageServices.EXE.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/CommonExtensions/Microsoft/TemplateProviders/Microsoft.VisualStudio.LanguageServices/Microsoft.VisualStudio.LanguageServices.EXE.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/CommonExtensions/Platform/Debugger/Microsoft.VisualStudio.LanguageServices.EXE.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/CommonExtensions/Platform/Debugger/Microsoft.VisualStudio.LanguageServices/Microsoft.VisualStudio.LanguageServices.EXE.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/CommonExtensions/Platform/DiagnosticsHub/Microsoft.VisualStudio.LanguageServices.EXE.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/CommonExtensions/Platform/DiagnosticsHub/Microsoft.VisualStudio.LanguageServices/Microsoft.VisualStudio.LanguageServices.EXE.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/PrivateAssemblies/DataCollectors/Microsoft.VisualStudio.LanguageServices.EXE.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/PrivateAssemblies/DataCollectors/Microsoft.VisualStudio.LanguageServices/Microsoft.VisualStudio.LanguageServices.EXE.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/PrivateAssemblies/DataCollectors/x86/Microsoft.VisualStudio.LanguageServices.EXE.
LOG: Download von neuem URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/Common7/IDE/PrivateAssemblies/DataCollectors/x86/Microsoft.VisualStudio.LanguageServices/Microsoft.VisualStudio.LanguageServices.EXE.
LOG: Download von neuem URL file:///C:/USERS/ME/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/15.0_15D7A44DEXP/EXTENSIONS/AUTHOR/MY ASSEMBLY/1.0/Microsoft.VisualStudio.LanguageServices.DLL.
LOG: Download von neuem URL file:///C:/USERS/ME/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/15.0_15D7A44DEXP/EXTENSIONS/AUTHOR/MY ASSEMBLY/1.0/Microsoft.VisualStudio.LanguageServices/Microsoft.VisualStudio.LanguageServices.DLL.
LOG: Download von neuem URL file:///C:/USERS/ME/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/15.0_15D7A44DEXP/EXTENSIONS/AUTHOR/MY ASSEMBLY/1.0/Microsoft.VisualStudio.LanguageServices.EXE.
LOG: Download von neuem URL file:///C:/USERS/ME/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/15.0_15D7A44DEXP/EXTENSIONS/AUTHOR/MY ASSEMBLY/1.0/Microsoft.VisualStudio.LanguageServices/Microsoft.VisualStudio.LanguageServices.EXE.

You find a minimal solution example in this ZIP file. Here, I created a new solution with a new VSIX project, added a ToolWindow via the wizard, added my vital code, resolved missing references, started debugging and received the same old FileNotFoundException.

Does anyone see the problem here? Thanks in advance.

Chris Tophski
  • 930
  • 1
  • 6
  • 23
  • Based on your description, it seems that vs-extension could not find some dll file, and I am not sure what causes the issue. please share a simple demo via OneDrive. – Zhanglong Wu - MSFT Jul 26 '17 at 02:43
  • You can also use fusionlog (https://learn.microsoft.com/en-us/dotnet/framework/tools/fuslogvw-exe-assembly-binding-log-viewer) to check where the extension is looking to find these assemblies. – utkarsh Jul 26 '17 at 20:58
  • @ColeWu-MSFT: I'll share a demo as soon as possible. – Chris Tophski Jul 30 '17 at 13:37
  • @utkarsh: The fusion log doesn't tell me much, I'll post the output. – Chris Tophski Jul 30 '17 at 13:38
  • @ColeWu-MSFT: I added this link to a minimal solution example in the last paragraph: https://1drv.ms/u/s!AqzjK9Mbz6Dpdg2-n5vGUNo6a5k – Chris Tophski Aug 03 '17 at 19:33
  • If someone needs a ZIP file with the NuGet packages included, this will be found here: https://1drv.ms/u/s!AqzjK9Mbz6DpdxJnQF2s6JjHlJY – Chris Tophski Aug 03 '17 at 19:51
  • I have download the project and restore the package, then I build the project, it works ok, I have not encounter any problem. – Zhanglong Wu - MSFT Aug 04 '17 at 10:21
  • Thanks for your feedback. So, I must have a problem with my VisualStudio installation or the VisualStudio SDK. I'll check that. – Chris Tophski Aug 04 '17 at 12:58
  • Meanwhile, I repaired my Visual Studio, reinstalled everything from extensibility and Roslyn, reset the experimental instance, but it still throws the exception. – Chris Tophski Aug 05 '17 at 21:14

2 Answers2

1

You must use the version of the libraries that Visual Studio uses or lower.

For Community 2017 they can be found in

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\CommonExtensions\Microsoft\ManagedLanguages\VBCSharp\LanguageServices

Now in Visual Studio 2017 version 15.2
Microsoft.CodeAnalysis.Workspaces.dll, Microsoft.VisualStudio.LanguageServices.dll, Microsoft.CodeAnalysis.EditorFeatures.Text have version 2.2.0.

So install or update them to version 2.2.0 in NuGet and no need to include them to vsix

Newer version of Microsoft.VisualStudio.LanguageServices you can include in VSIX by adding <ForceIncludeInVSIX>true</ForceIncludeInVSIX> to .csproj file:

<Reference Include="Microsoft.VisualStudio.LanguageServices, Version=2.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <HintPath>..\packages\Microsoft.VisualStudio.LanguageServices.2.3.1\lib\net46\Microsoft.VisualStudio.LanguageServices.dll</HintPath> <ForceIncludeInVSIX>true</ForceIncludeInVSIX> </Reference>

But then we'll get other errors.

System.InvalidCastException: 'Unable to cast object of type 'Microsoft.VisualStudio.LanguageServices.RoslynVisualStudioWorkspace' to type 'Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace

Visual Studio 2017 version 15.3 use 2.3.2 version. Your extension uses lower version 2.2.0 and will work there. If you update components in NuGet to 2.3.2, extension will work in 15.3 and not work in 15.2

So use 2.2.0 version now.

Oleg Z
  • 36
  • 1
  • 4
  • 1
    +1 for ``. Unfortunately this is just a dirty workaround and I get the same exception. There must be 2 libraries involved, so that there is a `RoslynVisualStudioWorkspace` derived from a `VisualStudioWorkspace` - I suspect the former one coming from the Visual Studio and the latter one being a class from the embedded assemblies. For these classes, this leads to not being matched as coming from the same inheritance hierarchy by the CLR. In the end, it doesn't work and it even seems like losing one's way while trying to solve this, therefore I cannot accept this answer as-is. – Chris Tophski Aug 07 '17 at 18:11
  • I wanted to add this as comment. But stackoverflow didn't give me because of low reputation – Oleg Z Aug 07 '17 at 18:40
  • yes, I can remember the same situation with me, some time ago. – Chris Tophski Aug 07 '17 at 18:53
  • It seems, I found the decision. Try to update Microsoft.CodeAnalysis.Workspaces to version 2.2.0. VS uses this. I got no error. Must be tested. VS dlls in C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\CommonExtensions\Microsoft\ManagedLanguages\VBCSharp\InteractiveComponents – Oleg Z Aug 07 '17 at 19:08
  • @OlegZ: Not bad. At least I can now see my tree be filled in the viewmodel. The view still is not populated, but I have already been at this point some weeks ago. Your last comments have been most helpful for this particular question, so I tend to accept your answer, if you update it. – Chris Tophski Aug 07 '17 at 19:41
  • @OlegZ: OK, WPF works now. The style hasn't been applied and the example's styling overwrote my template in a wrong way, which led to rendering nothing. I'm accepting your answer, because at least the version hint finally helped. You should add this to your answer. Thanks :) – Chris Tophski Aug 08 '17 at 21:53
  • @ChrisTophski: I did not quite understand what to add. I think here is the second question about versions: "Can we use newer versions of roslyn components in Visual Studio extensions? And how to avoid cast problems?" Maybe you issue something like this. I cannot exactly answer this question – Oleg Z Aug 09 '17 at 08:12
0

OK, I got it.

Do nothing but just clean up the vsix project's bin folder. The rest will work fine

enter image description here

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Moumit
  • 8,314
  • 9
  • 55
  • 59
  • I see I didn't mention it, but this is usually the second thing I do - right after creating a backup of the solution directory. So, congrats it worked for you, but it didn't solve the problem in my case. Thanks anyway. – Chris Tophski Feb 08 '18 at 10:14