Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Roslyn workspace.OpenSolutionAsync().Projects always empty?

Tags:

roslyn

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.

like image 881
MatthewSot Avatar asked Jul 31 '14 23:07

MatthewSot


2 Answers

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.

like image 52
Jason Malinowski Avatar answered Nov 20 '22 08:11

Jason Malinowski


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.

like image 25
lukejkw Avatar answered Nov 20 '22 09:11

lukejkw