【问题标题】:MSBuild 15: The "Error" task could not be instantiatedMSBuild 15:无法实例化“错误”任务
【发布时间】:2017-04-27 15:43:52
【问题描述】:

我正在尝试以编程方式构建一个使用 C#7 以及因此使用 MSBuild 15 的项目,但由于程序集引用不匹配,此任务似乎失败了。

这是我的代码:

        string projectFilePath = Path.Combine(args.Any() ? args.First() :@"C:\Users\newsoni\Documents\Visual Studio 2017\Projects\ConsoleApp2\ConsoleApp2.sln");

        ProjectCollection pc = new ProjectCollection();
        Dictionary<string, string> globalProperty = new Dictionary<string, string>();
        globalProperty.Add("Configuration", "Debug");
        globalProperty.Add("Platform", "x86");

        BuildParameters bp = new BuildParameters(pc);
        bp.Loggers = new ILogger[] { new Logger(), new ConsoleLogger(),  };
        BuildRequestData BuidlRequest = new BuildRequestData(projectFilePath, globalProperty, "4.0", new string[] { "Build" }, null);
        BuildResult buildResult = BuildManager.DefaultBuildManager.Build(bp, BuidlRequest);

这是错误信息:

C:\Users\newsoni\Documents\Visual Studio 2017\Projects\ConsoleApp2\ConsoleApp2.sln.metaproj : error MSB4127: The "Error" task could not be instantiated from the assembly "Microsoft.Build.Tasks.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a". Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly as the one installed on your computer and that your host application is not missing a binding redirect for Microsoft.Build.Framework. Unable to cast object of type 'Microsoft.Build.Tasks.Error' to type 'Microsoft.Build.Framework.ITask'.
C:\Users\newsoni\Documents\Visual Studio 2017\Projects\ConsoleApp2\ConsoleApp2.sln.metaproj : error MSB4060: The "Error" task has been declared or used incorrectly, or failed during construction. Check the spelling of the task name and the assembly name.

这是一个项目的链接,您可以使用它来重新创建问题:

https://drive.google.com/a/xibis.com/file/d/0B-mqMIMqm_XHcVRJQmtxQkd1b3c/view?usp=sharing

您必须将代码中的路径更改为您自己机器上的项目,但这是 VS 2017 或更早的项目似乎并不重要。

另一件事可能相关也可能不相关,我注意到此文件夹中的 Microsoft.WebApplication.Build.Tasks.Dll:

C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v15.0\WebApplications

似乎仍然引用 Microsoft.Build.Framework.dll 版本 14,而不是我预期的 15。

【问题讨论】:

  • 彻底重组的 VS 安装过程造成了很大的破坏。您想要的 MSBuild 版本存储在我机器上的 C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin 中。可能不是你的,但在附近。任何人应该如何在那里找到它,并且从一台机器到另一台机器始终如一地这样做,对我来说是一个很大的谜。这里没有文档、没有博客、没有答案。
  • @HansPassant 感谢汉斯。是的,你是对的。我找到了您提到的 DLL,但由于其他问题,我选择了 nuget 路线。我怀疑这个问题已经被善意但最终有害的动态插件加载代码复杂化了。
  • 你说得对,这可能是正确的方法。我怀疑它更多地与交付未经测试的代码有关,并且当有人大声喊叫时能够快速修复它。考虑为未来的受害者发布 Nuget 包的链接。
  • @HansPassant 嗨,汉斯。对不起,我想你误会了。我在引用正确的 MSBuild 版本时遇到了几个问题,只有在我走 nuget 路线之后,我才放弃并发布了这个问题。我还是没有办法。

标签: msbuild c#-7.0 msbuild-15


【解决方案1】:

原来我的测试项目中有两个问题。首先是由于项目的命名。

但是,由于引用不正确,还有第二个问题。要以编程方式使用 MSBuild 15,您必须安装以下软件包:

Install-Package Microsoft.Build -Version 15.1.1012
Install-Package Microsoft.Build.Framework -Version 15.1.1012
Install-Package Microsoft.Build.Tasks.Core -Version 15.1.1012
Install-Package Microsoft.Build.Utilities.Core -Version 15.1.1012
Install-Package Microsoft.Net.Compilers -Version 2.2.0

还有一个步骤很疯狂,完全无法发现。您现在必须添加对此 DLL 的引用,该引用应与您的解决方案文件夹相关:

packages\Microsoft.Net.Compilers.2.2.0\tools\Microsoft.Build.Tasks.CodeAnalysis.dll

【讨论】:

  • 谢谢!这对我有帮助。
  • Microsoft.Build.Tasks.CodeAnalysis.dll 对我来说也是缺失的部分!谢谢!
【解决方案2】:

我向 Microsoft 开了一张支持票,他们已确认这是 Visual Studio 2017 中的一个错误。

他们的目标是在 Visual Studio 的更新 3 中对此进行修复,但这可能会失败。

此问题跟踪错误:

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

目前没有针对此 API 的解决方法,但您可以使用 Process 作为替代方法调用 MSBuild exe。

【讨论】:

    【解决方案3】:

    如果您在 PC 上直接或通过 Visual Studio 安装了 MSBuild,正确的解决方法是使用包 Microsoft.Build.Locator,它将找到已安装的版本并使用它执行构建。

    安装这些包

    Microsoft.Build
    Microsoft.Build.Framework
    Microsoft.Build.Locator
    

    前 2 个是必需的,以便代码可以编译,但应从项目输出中排除。

    在您的应用程序启动中添加以下代码行,这只需运行一次。

    Microsoft.Build.Locator.MSBuildLocator.RegisterDefaults();
    

    删除app.config 中特定于Microsoft.Build 或其他构建库的任何其他绑定重定向。对Microsoft.Build.Locator 程序集的调用将确保这些重定向自动发生。

    参考文献

    【讨论】:

      猜你喜欢
      • 2017-11-18
      • 2012-03-19
      • 2014-07-27
      • 2019-11-03
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多