3

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)
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
Pratik Mehta
  • 1,310
  • 4
  • 15
  • 37
  • 1
    What's the `solutionPath`? sidenote: there is an overload `MSBuildWorkSpace.Create()` without any arguments which adds the empty dictionary for you. – Jeroen Vannevel May 13 '14 at 12:45
  • SolutionPath is full path "C:\Users\prmehta\Documents\Visual Studio 2013\Projects\PrismWinRT40\PrsimWinRT40.sln"... the solution is loading correctly(because solution object has few properties) but solution.Projects are not loading.. – Pratik Mehta May 13 '14 at 14:56
  • Are you sure there isn't a typo in the name of the solution ("PrsimWinRT40.sln" instead of "PrismWinRT40.sln")? – andyp May 13 '14 at 17:06
  • What type of project is it? Does it open normally in Visual Studio 2013? – Jason Malinowski May 13 '14 at 17:29
  • yes, there is no typo(thought it was mistake when I wrote here) :).. also its a Windows store projects.. Note: I am also using EnvDTE.DTE object and everything is working fine. means I get all the projects inside solution. But getting issue while using above code.. – Pratik Mehta May 13 '14 at 19:20
  • Are you trying to use this API within Visual Studio to get the solution information alongside EnvDTE.DTE? As in the "solutionPath" above is the path to the solution that's already open in VS? – Jason Malinowski May 13 '14 at 20:38
  • 1
    yes, I am using EnvDTE.DTE alongside with Roslyn API.. Till now everything was working fine because I didn't require to open the solution using Roslyn(this is require to implement FindAllReferences Functionality) Note: When I created new separate project and try to use above code its working fine. – Pratik Mehta May 14 '14 at 12:22

2 Answers2

2

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.

Community
  • 1
  • 1
Jason Malinowski
  • 18,148
  • 1
  • 38
  • 55
  • 3
    I am using `MSBuildWorkspace.OpenSolutionAsync(...)` in a console application and I experience the exact same problem; the Projects-property is always empty... – Matze Aug 22 '14 at 19:13
  • @Matze Same here – Sava B. Apr 26 '17 at 20:34
  • If you're having issues, please make new questions with details of your projects so somebody can help. There's a lot of reasons it can break, so trying to address that via comments isn't a good way to do it. – Jason Malinowski Apr 27 '17 at 01:54
1

Make sure that Microsoft.CodeAnalysis.CSharp.Workspaces package is installed. It helped me.