c# - 如何使用 BuildManager 在 Visual Studio 2017 (MsBuild 15) 上构建 .Net Core 项目或解决方案

标签 c# msbuild .net-core visual-studio-2017

我想构建一个简单的项目或解决方案(VS 2017,.NET Standard/.Net Core):

ProjectCollection pc = new ProjectCollection();
pc.DefaultToolsVersion = "15.0";            
ILogger logger = new ConsoleLogger();
pc.Loggers.Add(logger);
Dictionary<string, string> globalProperty = new Dictionary<string, string>();
BuildRequestData buildRequest = new BuildRequestData(fileName, globalProperty, null, new[] { target }, null);
BuildParameters buildParameters = new BuildParameters(pc)
{
    DefaultToolsVersion = "15.0",
    OnlyLogCriticalEvents = false,
    DetailedSummary = true,
    Loggers = new List<Microsoft.Build.Framework.ILogger> { logger }.AsEnumerable()
};
var result = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequest);
return result.OverallResult == BuildResultCode.Success;

但是构建失败并出现以下错误

MSBUILD : warning MSB4196: The "*.overridetasks" files could not be successfully loaded from their expected location "C:\Program Files\dotnet". Default tasks will not be overridden.
MSBUILD : warning MSB4010: The "*.tasks" files could not be successfully loaded from their expected location "C:\Program Files\dotnet". Default tasks will not be available.
D:\Build\workspace\Lx\Lx.sln.metaproj : error MSB4036: The "Message" task was not found. 
Check the following: 
1.) The name of the task in the project file is the same as the name of the task class. 
2.) The task class is "public" and implements the Microsoft.Build.Framework.ITask interface. 
3.) The task is correctly declared with <UsingTask> in the project file, or in the *.tasks files located in the "C:\Program Files\dotnet" directory.

似乎无法找到正确的目录之类的...... 我应该怎么做才能解决这个问题?

注意:我不想从其他目录复制文件。

最佳答案

似乎为了将 BuildManager 与 .Net Core/Visual Studio 2017/MsBuild 15 一起使用,您必须设置几个环境变量:

var msbuildRoot = @"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild";
var msbuildExe = Path.Combine(msbuildRoot, @"15.0\Bin\MsBuild.exe");
var sdkPath = Path.Combine(msbuildRoot, "Sdks"); 
Environment.SetEnvironmentVariable("MSBUILD_EXE_PATH", msbuildExe);
Environment.SetEnvironmentVariable("MSBUILDSDKSPATH", sdkPath);
Environment.SetEnvironmentVariable("MSBuildExtensionsPath", msbuildRoot);

请注意,应在访问 MsBuild 类之前设置前两个。这是因为它们被 BuildEnvironmentHelper 阅读了在静态初始化(单例模式)中。检查方法 TryFromEnvironmentVariable 了解更多详细信息。

但是最后一个 MSBuildExtensionsPath 可以通过全局属性设置:

globalProperties.Add("MSBuildExtensionsPath", msbuildRoot);

2020 年 2 月 28 日更新:

已找到 Buildalyzer repository . 似乎无法进行完整构建,但也许有一些解决方法可以避免弄乱环境。

关于c# - 如何使用 BuildManager 在 Visual Studio 2017 (MsBuild 15) 上构建 .Net Core 项目或解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43495509/

相关文章:

c# - XNA 人工智能 : Managing Enemies on Screen

c# - 使用LINQ计算每一行的总和

c# - 在 div 中包装 foreach 循环的一部分

delphi - 如何为 Delphi 项目执行 MSBUILD 的 'dry run'?

c# - 网络核心 : Generic Repository Primary Id Key Performance in Entity Framework

c# - 注销 - 如何清除缓存,asp.NET

c# - 构建错误 MC1000 'Object reference doen' t 设置为实例

msbuild - 如何解决 CruiseControl.NET 中 <msbuild> 任务的 "Only one project can be specified"错误

.net-core - Nuget 中的 VS 2017 RC : I Can not update NETStandard. 库

c# - 如何在 ASP.NET Core 1.1 中对使用 HttpContext 的 MVC Controller 进行单元测试