2017-05-29 69 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