【发布时间】: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