0

I am trying to enumerate all projects of a solution using Microsoft.VisualStuio.LanguageServices. This is working fine for VisualStudio 2019 but it is failing on VisualStudio 2017. My code is as bellow-

var workspace= ComponentModel.GetService<Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace>(); 
var projects = workspace.CurrentSolution.Projects;        

I am getting exception in the second line -

System.ArgumentNullException: 'Value cannot be null. Parameter name: source'

It shows list of projects on debug though. I am using Microsoft.VisualStudio.LanguageServices.dll of 2.10.0.0 version which is same as loaded dll in VS module. Is it a assembly issue? How can I fix this?

chaitali
  • 63
  • 5
  • based on my test, I find that I can not use the code you provided. Can you tell me what reference you added or nuget-package you added? – Jack J Jun Dec 30 '20 at 06:39
  • I just figured out the issue. Microsoft.VisualStudio.LanguageService.dll version 2.10.0 was not compatible with 3.7.0.0 version of Microsoft.CodeAnalysis.dll that I was referring. Visual Studio 2017 uses Microsoft.CodeAnalysis.dll version 2.10.0.0, though it does not load in Module. – chaitali Dec 30 '20 at 07:51

1 Answers1

0

This issue happened due to mismatch versions of Microsoft.CodeAnalysis between referenced dll and VS2017's dll.

In the module window it was clearly visible that, VS is using Microsoft.VisualStudio.LanguageServices 2.10.0.0, hence I kept the reference dll of same version.

In this statement- var workspace= componentModel.GetService<Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace>(); workspace is of type Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace.

But when we do this - var projects = workspace.CurrentSolution.Projects;

the type of variable 'projects' is IEnumerable<Microsoft.CodeAnalysis.Project>

I suppose, that is the reason while debugging the value was available in quick watch but failed in to execute.

Solution - refer dlls of same version as referred by VS.

chaitali
  • 63
  • 5