Search code examples
c#visual-studio-2017roslynvsixroslyn-code-analysis

Loading projects using Microsoft.VisualStudio.LanguageServices throws System.ArgumentNullException for Visual Studio 2017


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?


Solution

  • 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.