gpt4 book ai didi

MSBuild 15 : The "Error" task could not be instantiated

转载 作者:行者123 更新时间:2023-12-02 12:09:17 24 4
gpt4 key购买 nike

我正在尝试以编程方式构建一个使用 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。

最佳答案

事实证明我的测试项目存在两个问题。首先是由于项目的命名。

但是,还有第二个问题,这是由于引用不正确造成的。要以编程方式使用 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

关于MSBuild 15 : The "Error" task could not be instantiated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43662328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com