[英]Build project with Microsoft.Build API
我正在嘗試使用 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 項目中):
仍然得到相同的結果。
我也試過像這樣使用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問題:
以下是對我有用的內容:
我從我的項目輸出中刪除了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 列出的異常,並避免了大量關於無法解析所有引用的編譯器錯誤消息。
編輯:更多信息在這里: https : //gist.github.com/DustinCampbell/32cd69d04ea1c08a16ae5c4cd21dd3a3
Micrsoft 在這里提供了一個很好的鏈接
訣竅確實是使用 nuget pacakges,並指定 ExcludeAssets=runtime 來告訴 NuGet 程序集僅在構建時需要,不應復制到輸出目錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.