12

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

4 Answers 4

23

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

Sign up to request clarification or add additional context in comments.

1 Comment

Odd. I'll try again with a clean project.
7

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

1 Comment

This also answer this question
2

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.

Comments

-1

Install-Package Microsoft.Build.Utilities.Core as pointed out above worked for me. Done in a minute. Shortest fix. VS2022 / MSBUILD current.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.