Build project with Microsoft.Build API

microsoft.build.evaluation.project example
microsoft build assembly
msbuild api
microsoft.build.evaluation.project unload
a packagereference to microsoft build without excludeassets runtime exists in your project
microsoft build buildengine project
c# build project programmatically
update msbuild

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"}));

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

Project Class (Microsoft.Build.BuildEngine), I hit the same error after I installed: Install-Package Microsoft.Build -Version 15.1.​1012. But then I installed: Install-Package Microsoft.Build. In addition, NuGet also hosts a legacy assembly, Microsoft.Build.Engine, which is deprecated. There are several different versions of the MSBuild API, and for versions 15 and 16, there are two distinct forms of the assemblies in the NuGet packages, one compiled with the .NET Framework, and another compiled with .NET Core, which is a subset of the .NET Framework API surface.

There is apparently a new way to fix this problem, as described here and here.

Here's what worked for me:

  1. I deleted Microsoft.Build.dll, Microsoft.Build.Framework.dll, Microsoft.Build.Tasks.Core.dll and Microsoft.Build.Utilities.Core.dll (i.e., all Microsoft.Build .dll files) from my project's output folder.

  2. I deleted several Microsoft.Build NuGet packages from my project's references.

  3. I installed the Microsoft.Build.Locator NuGet package for my project.

  4. I added the following code to my program:

     // 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();
    

This fixed the exception listed by the OP and avoided the tons of compiler error messages about being unable to resolve all references.

Edit: More information here: https://gist.github.com/DustinCampbell/32cd69d04ea1c08a16ae5c4cd21dd3a3

MSBuild Class (Microsoft.Build.Tasks), NET SDK is located for new SDK-style projects: the SDK contains .targets files with the targets and tasks for the new project system. There are  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:

Micrsoft provides a good link here

The trick is indeed to use nuget pacakges, and to specify ExcludeAssets=runtime to tell NuGet that the assemblies are needed only at build time, and shouldn't be copied to the output directory.

Build project with Microsoft.Build API, The Microsoft Build Engine (MSBuild) is the build platform for . to host and review code, manage projects, and build software together. NET Core SDK. Build  Microsoft Build 2020: All developers need to know Watch Now Microsoft has unveiled a new effort to try to reduce the fragmentation surrounding its two Windows app platforms.

Running A Design-Time Build With MSBuild APIs, “The first edition of Inside the Microsoft Build Engine was a brilliant look at the internals of MSBuild, I could have used at least half of them in my development projects! Simply that ship with Team Build as well the Team Build API. Chapter​  API BUILD Programming Windows Windows has a bit of a fragmentation problem. Decisions made during the Windows 8 era have cascaded until, today, you have two branches of Windows apps: Those built for win32 desktop and Universal Windows Platform (UWP).

microsoft/msbuild: The Microsoft Build Engine (MSBuild) is , By invoking msbuild.exe on your project or solution file, you can orchestrate This is a CLI wrapper with fluent API that allows to modify the following arguments:. As well as all the workloads in the .NET 5 and .NET 6 wave, we are streamlining .NET for developers with a consistent API, consistent project system, and tooling. With this evolution comes an exciting evolution for native client developers building apps with .NET.

[PDF] Inside the Microsoft Build Engine: Using MSBuild , At this year's Microsoft BUILD, the company unveiled Project Reunion, That means uniting the existing win32 and UWP APIs, as well as  Project Reunion. Project Reunion is breaking down the barrier between Win32 and Universal Windows Platform (UWP) APIs. It starts with WinUI 3 Preview 1 and WebView2. WinUI makes it easy to build modern, seamless UIs for all Windows apps across both Win32 and UWP. It’s powered by a highly optimized C++ core and will never force you to update.

Comments
  • Odd. I'll try again with a clean project.
  • This also answer this question