Could not run the “GenerateResource” task

Few weeks ago I moved an existing custom build to VSTS build. In the process I moved build server too.

The new build is aimed to build applications from .NET framework 2.0 to .NET framework 4.7. Initially I created build agent on my dev machine. Within a week I was able to build everything on my local dev machine. As soon as I created a new agent on build server, build started failing. Of all projects, one project built on .NET framework 3.5 was real pain. A Windows Service targeting .NET 3.5 was throwing error message while compiling the project.

error MSB4216: Could not run the “GenerateResource” task because MSBuild could not create or connect to a task host with runtime “CLR2” and architecture “x64”

I researched (googled) 🙂 for whole day with few solutions in hand. Most common answer on almost every forum was suggesting below solution:

Go to your csproj file and add the following line under the default property group:

<PropertyGroup>
<DisableOutOfProcTaskHost>true </DisableOutOfProcTaskHost>
</PropertyGroup>

But I was not favor of changing anything in the project file as it was working perfectly fine with existing build system. So, I moved to second most favored solution which was replacing the msbuild path:

From: C:\Program Files (x86)\MSBuild\12.0\bin\amd64\MSBuild.exe
To: C:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe

The logical reason for this action was, “this occurs because the version on msBuild.exe has changed after Update 4 of vs2013.”

Again I didn’t wanted to do hard coding so moved on from this solution.

Third solution which I got and worked for me was creating a system variable (DISABLEOUTOFPROCTASKHOST) on build machine and assigning variable value as 1. Basically it was a bug in .NET 4.0. Somehow it stung me now 😉

I got the solution but I didn't stopped and wanted to understand the real reason of the error. After going through many forums and blog I understood that, the MSBuildTaskHost.exe was introduced by Visual Studio 2012 but not the Windows SDK. This was a new feature in VS 2012 build system, out of process build. One can disable this feature if VS 2012 is not used in building by setting environment variable: DISABLEOUTOFPROCTASKHOST=1

Steps to add system variable:

Click Start >> right-click Computer >> Properties >> Advanced system settings >> click Environment Variables button to open the dialog, then under the System variables section, click New… button, type the Variable name = DISABLEOUTOFPROCTASKHOST, and type the Variable value = 1,

This worked for me and I liked this approach as I didn’t touch/changed existing code and things works as expected.