强制 BuildManager 使用另一个版本的 MSBuild
- 作者: 大美人困兽之斗
- 来源: 51数据库
- 2023-02-13
问题描述
以下代码尝试使用 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:WINDOW**icrosoft.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、M??icrosoft.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.
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源