Opening a solution with msbuildworkspace gives diagnostics errors without details

*爱你&永不变心* 提交于 2019-12-22 11:18:44

问题


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.

They are created in Visual Studio 2017, .Net 4.6.2.

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.

The code for opening the solution:

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;

        }
    }
}

The version of Microsoft.CodeAnalysis is 2.0.0.0. Does anybody have any idea why MSBuild failed, how I can get more information ?


回答1:


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

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.



来源:https://stackoverflow.com/questions/43013142/opening-a-solution-with-msbuildworkspace-gives-diagnostics-errors-without-detail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!