MSBuild 15: The “Error” task could not be instantiated

早过忘川 提交于 2019-12-05 14:57:08

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

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

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

I opened a support ticket with Microsoft and they have confirmed that this is a bug in Visual Studio 2017.

They are aiming to get a fix for this into update 3 for Visual Studio, but this may slip.

This issue tracks the bug:

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

There is no workaround at present for this API, but you can invoke the MSBuild exe using Process as an alternative.

If you have MSBuild installed on your PC, either directly or through Visual Studio, the proper fix is to use the package Microsoft.Build.Locator which will find that installed version and execute the build using it.

Install these packages

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

The first 2 are needed so that the code can compile but should be excluded from the project output.

In your application startup add the following line of code, this only has to run once.

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

Remove any additional binding redirects in your app.config that are specific to Microsoft.Build or other build libraries. The call to the Microsoft.Build.Locator assembly will ensure that these redirects take place automatically.

References

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!