MSBuildWorkspace.OpenSolutionAsync throws an exception

Here is the complete code:

MSBuildWorkspace msBuild = MSBuildWorkspace.Create();
Solution sln = await msBuild.OpenSolutionAsync(solutionPath);//NullReferenceException

where solutionPath is a valid .sln Visual Studio 2015 solution. I tried both a Winforms new project and a dll with a simple class. I closed that solution before attempting to read it, but that didn't help. It still throws the NullReferenceException. I'm using VS2015. x64 Windows 10.

Is MSBuildWorkspace the wrong kind of Workspace? Is something else missing?

(NullReferenceException is actually a strange error to be thrown by a method. This is usually the type of error thrown by something like mistakenNull.DoSomething(); .)

EDIT 2 - This code is in a dll project in a solution which also contains a simple Winforms project which calls this code. (Since the Roslyn project template created a dll project.) Perhaps this matters.


EDIT - Stack Trace

The exception: Object reference not set to an instance of an object.

Stack Trace:

   at Microsoft.CodeAnalysis.CSharp.CSharpProjectFileLoader.CSharpProjectFile.GetReferences(CSharpCompilerInputs compilerInputs, ProjectInstance executedProject, IEnumerable`1& metadataReferences, IEnumerable`1& analyzerReferences)
   at Microsoft.CodeAnalysis.CSharp.CSharpProjectFileLoader.CSharpProjectFile.CreateProjectFileInfo(CSharpCompilerInputs compilerInputs, ProjectInstance executedProject)
   at Microsoft.CodeAnalysis.CSharp.CSharpProjectFileLoader.CSharpProjectFile.<GetProjectFileInfoAsync>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.<LoadProjectAsync>d__40.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.<GetOrLoadProjectAsync>d__39.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.<OpenSolutionAsync>d__34.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at namespace1.RosTest.<RenameMethods>d__0.MoveNext() in C:\Users\...


You need to make sure include Microsoft.CodeAnalysis.CSharp.Workspaces.dll and or Microsoft.CodeAnalysis.VisualBasic.Workspaces.dll beside your application.

The workspaces code loads them dynamically.