Getting Error MSB3086: Task could not find sgen.exe using the SdkToolsPath when building .Net 4.0 solution with TeamCity 8.0

There are those errors that can really ruin your day. And when you search Google for a solution, the only ones you get are for software versions of a few years ago. With this post I want to make sure I'll remember the solution in the future and hopefully others will also benefit from it.

I was preparing a new release of a .Net 4 Web Application project. When building the project on my TeamCity build server I got the following error:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(2769, 5): error MSB3086: Task could not find "sgen.exe" using the SdkToolsPath "" or the registry key "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v8.0A\WinSDK-NetFx40Tools-x86". Make sure the SdkToolsPath is set and the tool exists in the correct processor specific location under the SdkToolsPath and that the Microsoft Windows SDK is installed.

I had no idea where that came from, the project is built each night which was going good the last couple of days and I didn't remember doing anything out of the ordinary since yesterday. In desperation I googled the error and at first I stumbled on some StackOverflow questions that suggested to install Visual Studio 2010 as a solution.

Installing Visual Studio was no option, it's too bloaty to install on my Windows build VM. So I started roaming the other results the Google search had given me and stumbled on this thread on the Xamarin forums. Although the problem user WaynePhipps is talking about occurs in an Android project, the error he gets is exactly the same as I get. He states that the project failed to build once he added a web reference to it. Suddenly I remembered that that was one of the changes I had done that day.

If you ever add a web reference to your a project and the solution fails to build with the above error, there is pretty easy workaround. Make sure that you set your build configuration to the configuration in which TeamCity is building your solution. Most of the time this will be the "Release" build configuration.

Once the correct build configuration is set, go to the properties of the project file where a web reference was added. On the Build tab, set the 'Generate serialization assembly' drop down to 'Off'.

Project properties page

The project should now build in Teamcity.

As an alternative you can edit the *.csproj file manually, open the file in your favorite text editor, not Visual Studio, and add the following XML to the first <PropertyGroup> tag:

<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>

All credits of this solution go to user WaynePhipps on the Xamarin forums. I did not find a web presence of him but if you read this post, just shoot me an e-mail and I will gladly give you some Google karma.