强制 BuildManager 使用另一个版本的 MSBuild

时间:2023-02-18
本文介绍了强制 BuildManager 使用另一个版本的 MSBuild的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

以下代码尝试使用 BuildManager 以编程方式构建解决方案:

The following code tries to build a Solution programmatically, using BuildManager:

ProjectCollection pc = new ProjectCollection();
pc.DefaultToolsVersion = "12.0";
pc.Loggers.Add(fileLogger);
Dictionary<string, string> globalProperty = new Dictionary<string, string>();
BuildRequestData buildRequest = new BuildRequestData(solutionName, globalProperty, null, new[] { "Build" }, null);

BuildParameters buildParameters = new BuildParameters(pc)
{
    DefaultToolsVersion = "12.0",
    OnlyLogCriticalEvents = false,
    DetailedSummary = true,
    Loggers = new List<Microsoft.Build.Framework.ILogger> { fileLogger }.AsEnumerable()
};

var result = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequest);

当我运行这段代码时,它不会构建任何东西.我可以看到除了 winmdexp.exe 的一个特定版本之外,还使用了以下编译器 csc.exe:

When I run this code, it doesn't build anything. I can see that the following compiler csc.exe is being used, in addition to one particular version of winmdexp.exe:

C:WINDOWSMicrosoft.NETFrameworkv4.0.30319Csc.exe
ExportWindowsMDFile:
    C:Program Files (x86)Microsoft SDKsWindowsv8.0AinNETFX 4.0 Toolswinmdexp.exe 

但是当我使用 VS IDE 成功构建解决方案时,出现以下信息:

But when I successfully build a solution using VS IDE, the following information comes up:

C:Program Files (x86)MSBuild12.0inCsc.exe
ExportWindowsMDFile:
    C:Program Files (x86)Microsoft SDKsWindowsv8.1AinNETFX 4.5.1 Toolswinmdexp.exe

为什么在我的代码中会发生这种情况?怎么改?

Why is this happening in my code & how can I change it?

推荐答案

截至 Visual Studio 2013,MSBuild 不再是 .NET Framework 组件.这意味着对打包和部署进行了一些重组.旧的 MSBuild 框架仍然存在于 .NET Framework 文件夹中.但是,当您安装 Visual Studio 2013 时,Microsoft Build Tools 12.0 也会安装到 C:Program Files (x86)MSBuild12.0.请注意,构建工具与 Visual Studio 分开提供此处.

As of Visual Studio 2013, MSBuild is no longer a .NET Framework component. This meant some restructuring of packaging and deployment. The old MSBuild framework still lives in the .NET Framework folders. However when you install Visual Studio 2013, the Microsoft Build Tools 12.0 are also installed to C:Program Files (x86)MSBuild12.0. Note that the Build Tools are available separately from Visual Studio here.

当我第一次尝试这个时,我也遇到了和你一样的情况.问题是您可能已经引用了旧的4.0"MSBuild Framework 程序集.您需要引用位于 C:Program Files (x86)MSBuild12.0in 中的新 12.0 程序集(您必须在 VS 中浏览以添加引用).您可能需要 Microsoft.Build、Microsoft.Build.Engine 和 Microsoft.Build.Framework.更新这些参考资料后,我发现它在构建时使用了与 VS 2013 相同的工具.

I encountered the same situation as you as well when I first tried this. The issue is that you probably have referenced the old "4.0" MSBuild Framework assemblies. You need to reference the new 12.0 assemblies located in C:Program Files (x86)MSBuild12.0in (you'll have to browse there in VS to add the references). You probably need Microsoft.Build, Microsoft.Build.Engine, and Microsoft.Build.Framework. Once I updated those references I saw it was using the same tools as VS 2013 when building.

这篇关于强制 BuildManager 使用另一个版本的 MSBuild的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何在 Visual Studio 2017 项目(新的 .csproj 文件格式)中设置“OutputPath&quo 下一篇:在构建服务器上构建没有 Visual Studio 的 ASP.NET 4.5

相关文章

最新文章