If I'm reading your comments right, your code is running within Visual Studio, and you're trying to get a workspace to match whatever solution is currently open. In that case, do not use MSBuildWorkspace. MSBuildWorkspace really should never be used inside devenv.exe. Follow the answers to this question which lets you get direct access to the Roslyn model that we've already built up atop the solution model.
MSBuildWorkspace.Create seems to be not working properly
Question
I am trying to open the solution and want to refer the project.. but solution.Projects always comes empty. Below is my code. Am I missing something?
var workspace = MSBuildWorkspace.Create(ImmutableDictionary<string, string>.Empty);
var solution = workspace.OpenSolutionAsync(solutionPath).Result;
var project = solution.Projects.FirstOrDefault(p => p.Name == projectName); //Here solution.Projects comes empty(count 0)
Solution
OTHER TIPS
Make sure that Microsoft.CodeAnalysis.CSharp.Workspaces package is installed. It helped me.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow