MSBuild 15:无法实例化"错误"任务

Ian*_*son 4 msbuild c#-7.0 msbuild-15

我正在尝试以编程方式构建一个使用C#7的项目,因此使用MSBuild 15,但是这个任务似乎因为不匹配的程序集引用而失败.

这是我的代码:

        string projectFilePath = Path.Combine(args.Any() ? args.First() :@"C:\Users\newsoni\Documents\Visual Studio 2017\Projects\ConsoleApp2\ConsoleApp2.sln");

        ProjectCollection pc = new ProjectCollection();
        Dictionary<string, string> globalProperty = new Dictionary<string, string>();
        globalProperty.Add("Configuration", "Debug");
        globalProperty.Add("Platform", "x86");

        BuildParameters bp = new BuildParameters(pc);
        bp.Loggers = new ILogger[] { new Logger(), new ConsoleLogger(),  };
        BuildRequestData BuidlRequest = new BuildRequestData(projectFilePath, globalProperty, "4.0", new string[] { "Build" }, null);
        BuildResult buildResult = BuildManager.DefaultBuildManager.Build(bp, BuidlRequest);
Run Code Online (Sandbox Code Playgroud)

这是错误消息:

C:\Users\newsoni\Documents\Visual Studio 2017\Projects\ConsoleApp2\ConsoleApp2.sln.metaproj : error MSB4127: The "Error" task could not be instantiated from the assembly "Microsoft.Build.Tasks.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a". Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly as the one installed on your computer and that your host application is not missing a binding redirect for Microsoft.Build.Framework. Unable to cast object of type 'Microsoft.Build.Tasks.Error' to type 'Microsoft.Build.Framework.ITask'.
C:\Users\newsoni\Documents\Visual Studio 2017\Projects\ConsoleApp2\ConsoleApp2.sln.metaproj : error MSB4060: The "Error" task has been declared or used incorrectly, or failed during construction. Check the spelling of the task name and the assembly name.
Run Code Online (Sandbox Code Playgroud)

以下是可用于重新创建问题的项目的链接:

https://drive.google.com/a/xibis.com/file/d/0B-mqMIMqm_XHcVRJQmtxQkd1b3c/view?usp=sharing

您必须将代码中的路径更改为您自己计算机上的项目,但如果这是VS 2017项目或更早版本似乎并不重要.

另一件可能相关或不相关的事情,我注意到此文件夹中的Microsoft.WebApplication.Build.Tasks.Dll:

C:\ Program Files(x86)\ MSBuild\Microsoft\VisualStudio\v15.0\WebApplications

似乎仍然引用Microsoft.Build.Framework.dll版本14,而不是我预期的15.

Ian*_*son 7

事实证明我的测试项目有两个问题.第一个是由于项目的命名.

然而,还有第二个问题是由于引用不正确.要以编程方式使用MSBuild 15,您必须安装以下软件包:

Install-Package Microsoft.Build -Version 15.1.1012
Install-Package Microsoft.Build.Framework -Version 15.1.1012
Install-Package Microsoft.Build.Tasks.Core -Version 15.1.1012
Install-Package Microsoft.Build.Utilities.Core -Version 15.1.1012
Install-Package Microsoft.Net.Compilers -Version 2.2.0
Run Code Online (Sandbox Code Playgroud)

还有一个步骤是坚果并且完全无法发现.您现在必须添加对此DLL的引用,该引用应该与您的解决方案文件夹相关:

包\ Microsoft.Net.Compilers.2.2.0 \工具\ Microsoft.Build.Tasks.CodeAnalysis.dll