MSBuild 15:“错误"指示无法实例化任务 [英] MSBuild 15: The "Error" task could not be instantiated

查看:85
本文介绍了MSBuild 15:“错误"指示无法实例化任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm attempting to programmatically build a project which uses C#7, and therefore MSBuild 15, but this task is failing seemingly because of mismatched assembly references.

这是我的代码:

        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.

以下是项目的链接,您可以使用该项目重新创建问题:

Here is a link to a project you can use to recreate the issue:

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

您必须将代码的路径更改为您自己计算机上项目的路径,但是这与VS 2017项目或更早版本似乎无关紧要.

You will have to change the path in the code to a project on your own machine, but it doesn't seem to matter if this is a VS 2017 project or earlier.

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

One other thing that may or may not be relevant, I noticed that the Microsoft.WebApplication.Build.Tasks.Dll in this folder:

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

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

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

Still seems to be referencing Microsoft.Build.Framework.dll version 14, not 15 as I would have expected.

推荐答案

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

It turns out there are two issues in my test project. The first is due to the naming of the project.

但是,还有第二个问题,这是由于参考文献不正确所致.要以编程方式使用MSBuild 15,必须安装以下软件包:

However there is a second issue which is due to the references not being correct. To use MSBuild 15 programmtically you must install the following packages:

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的引用,该引用应相对于您的解决方案文件夹:

There is one more step which is nuts and completely undiscoverable. You must now add a reference to this DLL which should be relative to your solution folder:

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

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

这篇关于MSBuild 15:“错误"指示无法实例化任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆