roslyn: MSBuildWorkspace.Create().OpenSolutionAsync() throws NullReferenceException when run in wpf app

The following line works in a console app but not if run in a wpf or win form app. Using VS 2015, .NET 4.6 and 1.0.0 of the roslyn apis.

    private void Test()
    {
        var solution = MSBuildWorkspace.Create().OpenSolutionAsync("...").Result;
    }

Using in a window loaded event in wpf:

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        var solution = MSBuildWorkspace.Create().OpenSolutionAsync("...").Result;
    }

Stack trace: {System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.CodeAnalysis.CSharp.CSharpProjectFileLoader.CSharpProjectFile.GetReferences(CSharpCompilerInputs compilerInputs, ProjectInstance executedProject, IEnumerable1& metadataReferences, IEnumerable1& 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()

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 25 (16 by maintainers)

Most upvoted comments

Hello @Pilchie, in my case I didn’t have a library. Steps to reproduce:

  1. Create new console app on.NET 4.5.2 in VS2015
  2. in package manger console
Install-package Microsoft.CodeAnalysis.Workspaces.Common -version 1.0.0

After that package.config looks like:

  <package id="Microsoft.CodeAnalysis.Analyzers" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.CodeAnalysis.Common" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.CodeAnalysis.Workspaces.Common" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.Composition" version="1.0.27" targetFramework="net452" />
  <package id="System.Collections.Immutable" version="1.1.36" targetFramework="net452" />
  <package id="System.Reflection.Metadata" version="1.0.21" targetFramework="net452" />
  1. Code below compiles without any error or warning
var solution = MSBuildWorkspace.Create().OpenSolutionAsync("...").Result;
  1. Run time exception message
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()

Now comes the workaround fix

  1. In package manger console:
Install-package Microsoft.CodeAnalysis -version 1.0.0
  1. At this stage, packages.config looks like
  <package id="Microsoft.CodeAnalysis" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.CodeAnalysis.Analyzers" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.CodeAnalysis.Common" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.CodeAnalysis.CSharp" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.CodeAnalysis.CSharp.Workspaces" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.CodeAnalysis.VisualBasic" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.CodeAnalysis.VisualBasic.Workspaces" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.CodeAnalysis.Workspaces.Common" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.Composition" version="1.0.27" targetFramework="net452" />
  <package id="System.Collections.Immutable" version="1.1.36" targetFramework="net452" />
  <package id="System.Reflection.Metadata" version="1.0.21" targetFramework="net452" />
  1. App runs without any exceptions