c# - 使用 Microsoft.Build API 构建项目

标签 c# .net build msbuild

我正在尝试使用 Microsoft.Build 中的类构建一个项目。

代码是:

var project = new ProjectInstance(CS_PROJ_FILE);
project.Build();

但是它抛出以下异常:

Microsoft.Build.Shared.InternalErrorException occurred
  HResult=0x80131500
  Message=MSB0001: Internal MSBuild Error: Type information for Microsoft.Build.Utilities.ToolLocationHelper was present in the whitelist cache as Microsoft.Build.Utilities.ToolLocationHelper, Microsoft.Build.Utilities.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a but the type could not be loaded. unexpectedly null
  Source=Microsoft.Build

我尝试将以下内容添加到包中(在 net452 和 net7 项目中):

  • id="Microsoft.Build"版本="15.1.1012"
  • id="Microsoft.Build.Framework"版本="15.1.1012"
  • id="Microsoft.Build.Runtime"version="15.1.1012"
  • id="Microsoft.Build.Tasks.Core"版本="15.1.1012"
  • id="Microsoft.Build.Utilities.Core"version="15.1.1012"

仍然得到相同的结果。

我也尝试过使用 BuildManager,如下所示:

var buildManager = new BuildManager();
buildManager.Build(new BuildParameters(),
                   new BuildRequestData(new ProjectInstance(CS_PROJ_FILE), 
                                        new[] {"Build"}));

最佳答案

安装后我遇到了同样的错误:

Install-Package Microsoft.Build -Version 15.1.1012

但后来我安装了:

Install-Package Microsoft.Build.Utilities.Core -Version 15.1.1012

事情开始起作用。

有点困惑...

“dasMulli”向我指出了这个 stackoverflow 问题:

https://github.com/Microsoft/msbuild/issues/1889

关于c# - 使用 Microsoft.Build API 构建项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44249320/

相关文章:

c# - .NET 桌面应用程序(Google、Yahoo、Facebook ...)的单点登录

file - 如何在gradle zipTree中递归复制文件夹内容?

c# - ORA-06550: 调用 Oracle 存储过程时参数的数量或类型错误

c# - 如何在 WPF 中检索 DataGrid 内容的总高度?

ios - 错误 : Error code 65 for command: xcodebuild with args - trying to run ionic project on ios device

java - Gradle 1.12 缓存

c# - 带有 SQL 通配符的 String.Format 导致 Dapper 查询中断

c# - 为什么一些元组列表是排序的而不是其他的?

c# - 在 Windows 上查找用于打开特定文件类型的默认应用程序

c# - 使用对象的接口(interface)将对象列表转换为 IList