Create a project using the Microsoft.Build API

I am trying to create a project using classes in Microsoft.Build.

Code:

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

However, this throws 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 tried to add the following to packages (both in net452 and in the 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"

Get the same result anyway.

I also tried using BuildManager as follows:

 var buildManager = new BuildManager(); buildManager.Build(new BuildParameters(), new BuildRequestData(new ProjectInstance(CS_PROJ_FILE), new[] {"Build"})); 
+6
source share
2 answers

I hit the same error after installation:

 Install-Package Microsoft.Build -Version 15.1.1012 

But then I installed:

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

And it all started.

A bit confusing ...

I pointed this stackoverflow question to "dasMulli" at:

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

+6
source

There seems to be a new way to fix this problem, as described here and here .

Here is what worked for me:

  • 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 output folder.

  • I removed several Microsoft.Build NuGet packages from my project links.

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

  • 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 // /questions/2427800/roslyn-msbuildworkspace-compiling-a-netstandard-project-referencing-a-netstandard-project-throws-no-value-for-runtimemetadataversion-found/6284332#6284332 MSBuildLocator.RegisterDefaults(); 

This caught the exception pointed to by the OP and avoided the tone of compiler error messages about the inability to resolve all links.

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

0
source

Source: https://habr.com/ru/post/1268338/


All Articles