问题描述
我正在尝试使用Roslyn和MSBuildWorkspace分析解决方案.该解决方案是一种新的解决方案,其中包含2个类库项目,一个引用另一个.
I am trying to analyse a solution with Roslyn, with MSBuildWorkspace.The solution is a new solution, with 2 class library projects in them, one referencing the other.
它们是在Visual Studio 2017.Net 4.6.2中创建的.
They are created in Visual Studio 2017, .Net 4.6.2.
打开解决方案时,我在工作区中收到两个通用错误.诊断都是:处理文件' PathToProject '时,MSBuild失败诊断或输出窗口中没有其他内容,以指示为什么它无法处理项目文件.
When I open the solution, I receive two generic errors in workspace.Diagnostics, both are :Msbuild failed when processing the file 'PathToProject'There is nothing more in the diagnostics or output window, to indicate WHY it failed to process the project file.
打开解决方案的代码:
namespace RoslynAnalyse
{
class Program
{
static void Main(string[] args)
{
LocalAnalysis();
}
private static void LocalAnalysis()
{
var workspace = MSBuildWorkspace.Create();
var solution = workspace.OpenSolutionAsync(@"D:\Code\Roslyn\RoslynAnalyse\SolutionToAnalyse\SolutionToAnalyse.sln").Result;
var workspaceDiagnostics = workspace.Diagnostics;
}
}
}
Microsoft.CodeAnalysis的版本为2.0.0.0.有谁知道为什么MSBuild失败,如何获得更多信息?
The version of Microsoft.CodeAnalysis is 2.0.0.0.Does anybody have any idea why MSBuild failed, how I can get more information ?
推荐答案
当MSBuildWorkspace无法以这种方式打开项目或解决方案时,几乎总是这样,因为使用MSBuildWorkspace的应用程序不包含与msbuild.exe相同的绑定重定向.配置中有.
When MSBuildWorkspace fails to open a project or solution this way, it is almost always because the application using MSBuildWorkspace does not include the same binding redirects that msbuild.exe.config has in it.
MSBuild使用绑定重定向来允许任务(通常使用msbuild API库的不同版本已经编译的C#代码)全部使用当前的msbuild API.否则,msbuild会出现运行时加载失败.
MSBuild uses binding redirects to allow tasks (typically already compiled C# code using possibly different versions of msbuild API libraries) to all use the current msbuild API's. Otherwise, msbuild gets runtime load failures.
解决方案是将一个app.config文件添加到您的项目中,并将绑定重定向(msbuild.exe.config文件的assemblyBinding部分)复制到您的文件中.
The solution is to add an app.config file to your project and copy the binding redirects (the assemblyBinding section of the msbuild.exe.config file) into your file.
这篇关于使用msbuildworkspace打开解决方案会给出诊断错误,而没有详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!