Build project with Microsoft.Build API : <p>I'm trying to build a project using the classes in Microsoft.Build.</p>

<p>The code is:</p>

<pre><code>var project = new ProjectInstance(CS_PROJ_FILE);
project.Build();
</code></pre>

<p>However it's throwing the following exception:</p>

<pre class="lang-none prettyprint-override"><code>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
</code></pre>

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

<ul>
<li>id="Microsoft.Build" version="15.1.1012"</li>
<li>id="Microsoft.Build.Framework" version="15.1.1012"</li>
<li>id="Microsoft.Build.Runtime" version="15.1.1012"</li>
<li>id="Microsoft.Build.Tasks.Core" version="15.1.1012"</li>
<li>id="Microsoft.Build.Utilities.Core" version="15.1.1012"</li>
</ul>

<p>Still get the same result.</p>

<p>I've also tried using the <code>BuildManager</code> like this:</p>

<pre><code>var buildManager = new BuildManager();
buildManager.Build(new BuildParameters(),
                   new BuildRequestData(new ProjectInstance(CS_PROJ_FILE), 
                                        new[] {"Build"}));
</code></pre>
