2017-05-29 71 views
4

我想使用Microsoft.Build中的類來構建項目。使用Microsoft.Build API構建項目

的代碼是:

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」 版本= 「1012年1月15日」
  • id =「Microsoft.Build.Framework」version =「15.1.1012」
  • id =「Microsoft.Build.Runtime」version =「15.1.1012」
  • id =「Microsoft.Build.Tasks.Core」版本= 「1012年1月15日」
  • ID = 「Microsoft.Build.Utilities.Core」 版本= 「1012年1月15日」

仍然得到同樣的結果。

我使用BuildManager這樣也試過:

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

回答

5

我打了同樣的錯誤後,我安裝:

Install-Package Microsoft.Build -Version 15.1.1012 

但後來我安裝:

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

事情開始奏效。

有點混亂......

我在指出這個問題計算器由 「dasMulli」:

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

+0

奇。我會再次嘗試一個乾淨的項目。 – BanksySan