MSBuildWorkspace OpenSolutionAsync() exits process

In a console application I have been trying to get OpenSolutionAsync to work for a couple of weeks now without success.

Finally I remembered to look at the roslyn-sdk repository which contains samples. I locally run the sample called SolutionExplorer and it works perfectly. I can choose a solution file and it can open the solution.

Given that this sample works, I decided to make my console app similar to it. So I copied the MSBuildService and WorkspaceService (just changing the logging behavior) to my console application. Changed my console app to target v4.6.1 of .NET Framework. Referenced the exact same nuget packages. I made sure to delete the bin folders and also compared the bin folder from the sample with the bin folder generated for my project. They are the same. And yet, when I hit the line to OpenSolutionAsync(), my console application just exits the process.

What am I doing wrong?

More info: I copied the project here to make it easily reachable for you. Please provide two parameters to run the project e.g:

callerid.scanner.exe -p "<path to some solution>" -d "<documentNameToFindInSolution>"

It exits the process on line 97 of WorkspaceService


A few things, sorry it VB but you should get the idea. The important thing is to get the right instance of MSBuild and trap the errors.

Property MSBuildInstance As VisualStudioInstance
' Get all instances of MSBuild
Private ReadOnly visualStudioInstances() As VisualStudioInstance = MSBuildLocator.QueryVisualStudioInstances().ToArray()
' Pick the instance of MSBuild you want
MSBuildInstance = visualStudioInstances(???)
MSBuildLocator.RegisterInstance(MSBuildInstance)
Using Workspace As MSBuildWorkspace = MSBuildWorkspace.Create()
    AddHandler Workspace.WorkspaceFailed, AddressOf MSBuildWorkspaceFailed
    Dim currentProject As Project = Workspace.OpenProjectAsync(FileName).Result
'  Do something with the project
End Using

' Print message for WorkspaceFailed event to help diagnosing project load failures.
 Private Sub MSBuildWorkspaceFailed(_1 As Object, e1 As WorkspaceDiagnosticEventArgs)
    If MsgBox(e1.Diagnostic.Message, MsgBoxStyle.AbortRetryIgnore, "MSBuild Failed") = MsgBoxResult.Abort Then
        End
    End If
End Sub

Hottest 'roslyn' Answers, MSBuildWorkspace OpenSolutionAsync() exits process. You need to register a msbuild location with the MSBuildLocator class. This will allow the� When a project is opened, MSBuildWorkspace loads it by using MSBuild to perform a design-time build of the project. This is a special build that retrieves all the necessary information to analyze the project (such as source files, references, compilation options, etc.), but stops short of actually compiling the project and emiting a binary on disk.


You need to register a msbuild location with the MSBuildLocator class. This will allow the MSBuildWorkspace to open solutions and projects. Long story short: it needs to know where msbuild is because it uses it to load projects.

I would place the following code in the constructor of your service

// Attempt to set the version of MSBuild.
var visualStudioInstances = MSBuildLocator.QueryVisualStudioInstances().ToArray();
var instance = visualStudioInstances.Length == 1
    // If there is only one instance of MSBuild on this machine, set that as the one to use.
    ? visualStudioInstances[0]
    // Handle selecting the version of MSBuild you want to use.
    : SelectVisualStudioInstance(visualStudioInstances);

_logger.WriteLine($"Using MSBuild at '{instance.MSBuildPath}' to load projects.");

// NOTE: Be sure to register an instance with the MSBuildLocator 
//       before calling MSBuildWorkspace.Create()
//       otherwise, MSBuildWorkspace won't MEF compose.
MSBuildLocator.RegisterInstance(instance);

MSBuildWorkspace.Create().OpenSolutionAsync() throws , OpenSolutionAsync() throws NullReferenceException when run in wpf app # 4127. Closed. GaryStonerock opened this issue on Jul 26, 2015 � 25 comments. MSBuildWorkspace.Create().OpenSolutionAsync() Project.Documents empty #19747. piotr-polanski opened this issue May 24, 2017 · 2 comments Assignees. Labels. Area-IDE


Found the problem. I was using nuget package CommandLineParser. It has to do with usage of async while triggering the action with CommandLineParser. When I removed async and called OpenSolutionAsync().Result instead, it works.

It seems like CommandLineParser has no support for async implementation. Then OpenSolutionAsync just quits the process when called with await within CommandLineParser's WithParsed method.

This cost me weeks to figure out.. Thanks for your answers anyways, @Jonathon Marolf and @Paul Cohen.

Using MSBuildWorkspace � GitHub, then add that reference in CreateProject() Thanks! https://stackoverflow.com/ questions/59103996/msbuildworkspace-opensolutionasync-exits-process. As a result of that, some projects can only be built with full framework MSBuild, so an application that wants to use the MSBuild API for them (including indirectly through MSBuildWorkspace) must target .NET Framework and use full-framework MSBuild. @d-dizhevsky Looking through this thread, I see several related issues. Can you be explicit


dotnet/roslyn, /questions/59103996/msbuildworkspace-opensolutionasync-exits-process when I call RemoveNodes() , I keep getting an ArgumentNullException buried� MSBuildWorkspace, along with its unit tests and other tests that happen to use it, directly or indirectly depend on being able to find the location of MSBuild binaries, settings, and targets in the registry. This was fine for MSBuild 14.0, but no such information is available for 15.0 (by design).


c#, MSBuildWorkspace OpenSolutionAsync() exits process � c# msbuild roslyn projects-and-solutions � Dec 1 '19 at 18:04 tubakaya. 0. 1� Not sure if you are asking specifically about MSBuildWorkspace or Roslyn in general. If it is about Roslyn Microsoft.CodeAnalysis.CSharp.Workspaces 2.3.1 has worked well for analyzing C# 7. If you are using nuget it should work out-of-the-box as nuget defaults to lowest matching for transient dependencies.


Newest 'projects-and-solutions' Questions, the comment is not a code exactly, but still Roslyn processes it. And to The Workspace and MSBuildWorkspace namely to help me. MainAsync(string[] args) { var workspace = await GetWorkspace(). OpenSolutionAsync(solution). Bit of writing to console (for humans, probably) and exit codes (for� When we try to open such a solution using MSBuildWorkspace.OpenSolutionAsync(solutionPath).Result, we get exception. NOTE When we open the above solution using Visual Studios 2013 and above, we get a Review Solution Action message window which suggests that The project(s) will be upgraded to use the Microsoft Studios 2013/2015 compiler and