I am using .Net 4.8 Console project with such NuGet packages
Microsoft.CodeAnalysis.CSharp.3.8.0
Microsoft.CodeAnalysis.CSharp.Workspaces.3.8.0
Microsoft.CodeAnalysis.Workspaces.MSBuild.3.8.0
Program.cs
MSBuildWorkspace workspace = MSBuildWorkspace.Create();
Solution currSolution = workspace.OpenSolutionAsync(@"C:\Users\***\source\repos\WpfApp5\WpfApp5.sln").Result;
OpenSolutionAsync throwing exception
Object reference not set to an instance of an object.
Stack trace
at Microsoft.Build.Shared.BuildEnvironmentHelper.get_Instance()
at Microsoft.Build.Utilities.Traits.get_Instance()
at Microsoft.Build.Evaluation.ProjectCollection..ctor(IDictionary`2 globalProperties, IEnumerable`1 loggers, IEnumerable`1 remoteLoggers, ToolsetDefinitionLocations toolsetDefinitionLocations, Int32 maxNodeCount, Boolean onlyLogCriticalEvents, Boolean loadProjectsReadOnly)
at Microsoft.CodeAnalysis.MSBuild.Build.ProjectBuildManager.StartBatchBuild(IDictionary`2 globalProperties)
at Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader.Worker.<LoadAsync>d__19.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader.<LoadSolutionInfoAsync>d__21.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.<OpenSolutionAsync>d__24.MoveNext()
I found only this issue and its looks like this error already not fixed for 5 years? Is there any solution or workaround that allows to open solution?
I was facing a similar issue in a console app (.NET framework 4.7.2). I followed the instructions listed in Using MSBuildWorkspace and it solved the issue for me.
The solution was to remove all Microsoft.Build.* references other than Microsoft.Build.Locator, and to add MSBuildLocator.RegisterDefaults(); before attempting to create a workspace.
This is what the code looks like:
using Microsoft.Build.Locator;
using Microsoft.CodeAnalysis.MSBuild;
...
MSBuildLocator.RegisterDefaults();
using (var workspace = MSBuildWorkspace.Create())
{
// Use MSBuildWorkspace...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With