无法加载程序集 Microsoft.Build.Utilities.Core 版本=15.1.0.0

Could not load assembly Microsoft.Build.Utilities.Core Version=15.1.0.0

我在 运行 转换配置文件的 gulp 任务中遇到问题。它报告以下错误:

error MSB4062: The "TransformXml" task could not be loaded from the assembly C:\Program Files (x86)\Microsoft Visual Studio19\Professional\MSBuild\Microsoft\VisualStudio\v16.0\Web\Microsof t.Web.Publishing.Tasks.dll. Could not load file or assembly 'Microsoft.Build.Utilities.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. Confi rm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.

任务的作用:

  1. 它创建一个 _msbuild.proj 文件,其中包含要转换的配置文件。该文件看起来像这样(显然 Source、Transform 和 Destination 都填充了真实数据):
<Project ToolsVersion="4.0" DefaultTargets="Demo" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <UsingTask TaskName="TransformXml" AssemblyFile="C:\Program Files (x86)\Microsoft Visual Studio19\Professional\MSBuild\Microsoft\VisualStudio\v16.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>
    <Target Name="Transform">
        <TransformXml Source="" Transform="" Destination=""/>
    </Target>
</Project>
  1. 它针对 msbuild 运行此文件:C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe .\_msbuild.proj /t:Transform

我已经看过了this answer which suggests it is a nuget package and might be corrupted。我认为情况并非如此,因为我项目中的其他人都没有这个问题。我也确实有有问题的 dll - Microsoft.Build.Utilities.Core 但是在更高版本 (16.4.0) 中安装在不同的地方以及 dotnet 和 VS 安装。

我看到的唯一区别是我使用的是 VS 2019,并且 Microsoft.Web.Publishing.Tasks.dll(一个包含 TransformXml 任务并依赖于缺少的库的库)在磁盘上的不同位置与 gulp 转换模块 - 我有它在 C:\Program Files (x86)\Microsoft Visual Studio 而其他人有它在 C:\Program Files (x86)\MSBuild

我在这个问题上卡了快一天了,一点动弹不得。

Could not load assembly Microsoft.Build.Utilities.Core Version=15.1.0.0

我认为问题是你使用的是Framework版本的msbuild.exe,而不是VS2019的msbuild。

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe .\_msbuild.proj /t:Transform表示你使用的是Framework的msbuild。

解释的更详细:

VS2019的MSBuild集成了多种vs环境参数,直接关系到你在vs中的各种操作,框架的msbuild只有最基本的编译工程功能没有 TransformXml 任务。

另外Microsoft.Web.Publishing.Tasks.dll只是VS2019的msbuild的一部分,所以可以被VS2019的msbuild识别。但是这个dll不在C:\Windows\Microsoft.NET\Framework64\v4.0.30319\xxxx下,所以用Framework的msbuild得不到你想要的。

解决方案

作为解决方法,您应该使用 VS2019 的 MSBuild 对应其文件夹中存在的 Microsoft.Web.Publishing.Tasks.dll

1)使用C:\Program Files (x86)\Microsoft Visual Studio19\Professional\MSBuild\Current\Bin\MSBuild.exe执行_msbuild.proj.

2) 或者直接使用 Developer Command Prompt

而且我认为其他人也使用过此 msbuild。

希望对您有所帮助。

在我的例子中,当我的脚本试图使用旧版本的 Nuget 来恢复包时,这个错误发生了。

我下载了Nuget 5.5,是兼容VS 2019的(在VS2019的帮助>关于中可以看到),问题消失

您可以从以下网址下载: https://www.nuget.org/downloads