MatthewSot
MatthewSot

Reputation: 3594

Roslyn workspace.OpenSolutionAsync().Projects always empty?

I'm trying to create a self-hosted WebAPI 2.0 project that allows you to open/explore/build .sln solutions through an API.

Here's the code within one of my controllers, that's supposed to return a list of projects given a path to the .sln:

public async Task<IHttpActionResult> GetProjects(string slnPath = "")
{
    var workspace = MSBuildWorkspace.Create();
    var solution = await workspace.OpenSolutionAsync(slnPath);
    var projects = solution.Projects;
}

I would expect projects to hold the projects in the solution, but according to the debugger, solution.Projects and solution.ProjectIds always seem to be empty.

I've tried this with multiple .sln files, all of which I can open in Visual Studio and see that they have projects in them.

I've seen this question, but my project isn't a Visual Studio add in, it's a class library called from a command line application.

Upvotes: 25

Views: 6122

Answers (2)

lukejkw
lukejkw

Reputation: 1134

If you are getting this issue in unit tests but not in your production code, ensure that you have referenced both Microsoft.CodeAnalysis.CSharp.dll and Microsoft.CodeAnalysis.CSharp.Workspaces in your test project.

JYL mentioned it in the comments of the selected answer but I didnt see it right away.

Upvotes: 7

Jason Malinowski
Jason Malinowski

Reputation: 19021

This is generally caused by one of a few things, in order of commonality:

  1. You are missing copies of Microsoft.CodeAnalysis.CSharp.Workspaces.dll or Microsoft.CodeAnalysis.VisualBasic.Workspaces.dll. Make sure when you are running your project that those DLLs are next to the main Microsoft.CodeAnalysis.Workspaces.dll.

  2. You're loading solutions with project types we don't support. We should support any project type except the project-less Web Site projects. Class libraries should work just fine.

  3. We have a bug that's causing us to mis-handle your particular project types. If that's the case, file a bug on GitHub.

Upvotes: 33

Related Questions