使用Microsoft.Build API构建项目
我正在尝试使用Microsoft.Build中的类来构建项目。
代码是:
var project = new ProjectInstance(CS_PROJ_FILE); project.Build();
然而,它抛出以下exception:
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项目中):
仍然得到相同的结果。
我也试过像这样使用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
显然有一种解决这个问题的新方法,如此处和此处所述 。
这对我有用:
-
我从项目的输出中删除了
Microsoft.Build.dll
,Microsoft.Build.Framework.dll
,Microsoft.Build.Tasks.Core.dll
和Microsoft.Build.Utilities.Core.dll
(即所有Microsoft.Build
.dll文件)夹。 -
我从项目的引用中删除了几个
Microsoft.Build
NuGet包。 -
我为我的项目安装了
Microsoft.Build.Locator
NuGet包。 -
我在程序中添加了以下代码:
// This needed after upgrading to Roslyn revision 34025, see these two links: // https://github.com/dotnet/roslyn/issues/26029 // https://stackoverflow.com/a/49886334/253938 MSBuildLocator.RegisterDefaults();
这修复了OP列出的exception,避免了大量无法解析所有引用的编译器错误消息。
编辑:更多信息: https : //gist.github.com/DustinCampbell/32cd69d04ea1c08a16ae5c4cd21dd3a3
上述就是C#学习教程:使用Microsoft.Build API构建项目分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1021555.html