MSBuildWorkspace.OpenSolutionAsync抛出异常

时间:2015-11-16 18:23:25

标签: c# .net roslyn

以下是完整的代码:

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

其中solutionPath是有效的 .sln Visual Studio 2015解决方案。我尝试了一个Winforms新项目和一个简单类的dll。我在尝试阅读之前关闭了该解决方案,但这没有帮助。它仍然抛出NullReferenceException。 我正在使用VS2015。 x64 Windows 10。

MSBuildWorkspace Workspace是错误的吗?还缺少其他东西吗?

(NullReferenceException实际上是一个由方法抛出的奇怪错误。这通常是像mistakenNull.DoSomething();.

这样的错误类型。

编辑2 - 此代码位于解决方案的dll项目中,该解决方案还包含一个调用此代码的简单Winforms项目。 (因为Roslyn项目模板创建了一个dll项目。)也许这很重要。

<小时/>

编辑 - 堆栈跟踪

异常:对象引用未设置为对象的实例。

堆栈跟踪

   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\...

2 个答案:

答案 0 :(得分:1)

您需要确保在申请旁边加{4}}或Microsoft.CodeAnalysis.CSharp.Workspaces.dll

工作区代码动态加载它们。

答案 1 :(得分:0)

我遇到了同样的问题,当我安装了正确的版本时,这个问题得到了解决 &#34; Microsoft.CodeAnalysis.CSharp&#34;从以下链接汇编。

https://www.nuget.org/packages/Microsoft.CodeAnalysis.CSharp/

这可能对某人有帮助,所以我在这里发帖。