Monday, August 19, 2013

Building LightSwitch HTML Clients with TFS Builder Agent

LightSwitch HTML Client is now included in Visual Studio 2012 Update 3. It is really a very nice thing when you want to build a tool quickly for internal use. The problem is that it does not play nice with TFS build agents.

First of all, if you are running a 64 bit server, MSBuild usually defaults to use its 64 bit version. The problem is that "Microsoft.VisualStudio.Settings.11.0.dll" that comes with Visual Studio 2012 SDK has only a 32 bit version. So when the TFS build agent runs, you will get errors saying that "The system cannot find the file specified. File name: 'Microsoft.VisualStudio.Settings.11.0, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'...". The file is actually right there in the Visusal Studio SDK common assembly folder. you can even add it to the GAC. It will not help because it is a 32 bit assembly. You will have to switch to the 32 bit MSBuild in your build definition:

Build Definition Setting for Building LightSwitch Applications
Once you set the MSBuild platform to "X86", the problem goes away. By the way, this does not change the build platform setting in your solution. It only applies to the MSBuild process. So everything works out as far as fixing the missing DLL problem.

The problems for building LightSwitch applications do not end there, unfortunately. Once you get pass the DLL problem, you will most likely see that the build agent starts to complain that the server output is missing: "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\LightSwitch\v3.0\Microsoft.LightSwitch.targets (108): Could not copy the file "...\obj\Release\Application.Server.dll" because it was not found.

Usually TFS build agent would build the release configuration of a solution. With LightSwitch projects (*.ls3proj) files, there are actually two sub-projects; one for the server and one for the HTML client. When building the LightSwitch project using MSBuild, the selected build configuration information gets lost. It never gets passed to the two sub-projects. When no build configuration is specified, these projects default to debug builds. When the time comes for the "ls3proj" to copy the output files, they are created in the wrong configuration, any you will get the errors described above.

A quick fix for this is to change the default configuration of these sub-projects. In the .Server folder, open the "*.csproj" file with NotePad, and change the default configuration to "Release":

Server Project File

Similarly, you can change the HTML client default to release as well. In the .HTMLClient folder, open the "*.jsproj" file with NotePad, and add a default configuration that is set to "Release":

HTML Client Project File

Of course, a better solution would be to fix "Microsoft.LightSwitch.Build.Tasks.targets" file to pass selected configuration to the sub-projects. That is a little more complicated. Since I do not need to create LightSwitch projects very often, fixing the project files is a much simpler solution.