Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

c# - Build project with Microsoft.Build API

I'm trying to build a project using the classes in Microsoft.Build.

The code is:

var project = new ProjectInstance(CS_PROJ_FILE);
project.Build();

However it's throwing the following 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

I've tried adding the following to the packages (both in a net452 and a net7 project):

  • id="Microsoft.Build" version="15.1.1012"
  • id="Microsoft.Build.Framework" version="15.1.1012"
  • id="Microsoft.Build.Runtime" version="15.1.1012"
  • id="Microsoft.Build.Tasks.Core" version="15.1.1012"
  • id="Microsoft.Build.Utilities.Core" version="15.1.1012"

Still get the same result.

I've also tried using the BuildManager like this:

var buildManager = new BuildManager();
buildManager.Build(new BuildParameters(),
                   new BuildRequestData(new ProjectInstance(CS_PROJ_FILE), 
                                        new[] {"Build"}));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I hit the same error after I installed:

Install-Package Microsoft.Build -Version 15.1.1012

But then I installed:

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

And things started working.

A little confusing...

I was pointed to this stackoverflow question by "dasMulli" at:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...