Integrating hosting web site creation in an automated LightSwitch factory

Introduction

Deploying LightSwitch apps is relatively straightforward and can be automated pretty well. Obviously you need an IIS server up and running, but you also need an installed web site (with a dedicated port number).  Obviously, it’s not that difficult to fist go the IIS and setup such a web site. Nonetheless, when your LightSwitch application deployment is embedded in a more automated pipeline, it makes sense to also automate this step.

This is simpler than it sounds at first glance. We’ll try to make use as much as possible web deploy since that’s also the primary technology when deploying LightSwitch applications.

I’ll focus in this post not on integrating the automated web site creation in the larger pipeline, but solely on how to automate website creation.

Step 1: Create a Website Template

This step, you’ll need to do only once and you will reuse the result (the website template package) in the following steps.

Open IIS and create a simple web site and assign a http port (I’ll skip on purpose https but that will become more clear later on).

Give it as site name: “TemplateWebSite” and assign port 1. Don’t worry about the fact that it’s number 1. Anyhow, in a minute or two you can delete this site and the port numbers for you new sites will be re-assigned. (you can chose the port number)

Make sure the site is in a physical path of your preference (but you need to remember this path)

 

Add also a binding for https:

Export now this template by clicking on the root of your server and select Deploy – Export server package :

 

Click now on manage components:

 

And delete the webserver entry and add a new provider “appHostConfig” and make sure that patch refers to the name of our templateWebsite:

 

In the next screen, uncheck the root and machine config files:

 

Click on advanced settings and provide an encryption password. Since we have only http as binding, this is strictly speaking not necessary. In case we would have https, there would be certificate info in the package, and as a result there would be the  need for encrypting this section.

Click next and save the package on disk and give it as name: “TemplateWebsitepackage.zip”. You will be probably asked to specify again a password. Take same password as above.

Create now an xml file and call it DeclareParameters.xml:

<parameters>
  <!-- declare parameter for Http Binding -->
  <parameter name="Site-http" description="Site Http Binding">
    <parameterEntry kind="DestinationBinding" scope="Site" match=":1:" />
  </parameter>
 <parameter name="Site-https" description="Site Https Binding">
    <parameterEntry kind="DestinationBinding" scope="Site" match=":2:" />
  </parameter>
  <parameter name="Application Pool" description="app pool">
    <parameterEntry kind="DeploymentObjectAttribute" scope="appHostConfig" match="application/@applicationPool" />
  </parameter>
</parameters>

Create a command file and call it PrepareForParameters.cmd and give following content:

“C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe” -verb:sync -source:package=”f:\TemplateWebsitepackage.zip”,encryptpassword=”secret” -dest:package=”f:\ParameterReadyWebSitePackage.zip”,encryptpassword=”secret” -declareParamFile:”f:\declareparameters.xml”

pause

 

Run now this command file as administrator  (obviously update the references to my F: drive to something you like) and you will see you have now a new zip file on your drive ParameterReadyWebSitePackage.zip.

Just in case you wanted to give up: until now, this has been work you only need to do once !

Step 2: Create a template application pool

We want for every lightswitch on our IIS, a dedicated app pool. So, we need to make also a template for an application pool

Create a new app pool with follwoing specifications (.net 4 and Integrated pipeline mode):

 

Right click again on the server node and create a new server package (same approach as above), but this time make sure you select as provider name “appPoolConfig” and provide as path the name of the app pool (TemplateAppPool):

Provide again a password for the encryption.

Step 3: deploy an application pool for a new website

Run following command as administrator:

“C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe” -verb:sync -source:package=”f:\TemplateAppPool.zip”,encryptpassword=”secret” -dest:appPoolConfig=”MyNewAppPool”,computerName=localhost

pause

You can see now that you have your new app pool:

 

Step 4: Deploy a custom website based on your template website.

Create now a new xml file and call it Parameters.xml and provide following content:

<parameters>
  <setParameter name="Site-http" value="*:101:"/>
  <setParameter name="Site-https" value="*:1010:"/>
  <setParameter name="Application Pool" value="MyNewAppPool"/>
</parameters>

As you can see we our l use http port 101 and we’ll use our newly created app pool “MyNewAppPool”.
Run now following script as administrator:

“C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe” -verb:sync -source:package=”f:\ParameterReadyWebSitePackage.zip”,encryptpassword=”secret” -dest:appHostConfig=”NEWSITE”,computerName=localhost -setParamFile=”f:\Parameters.xml” -replace:objectName=virtualDirectory,targetAttributeName=physicalPath,match=”^C:\\inetpub\\TemplateWebSite”,replace=”C:\inetpub\NEWSITE”

pause

Note that the script contains the name for our new website (in my case: NEWSITE) and also the physical path.

It would be more elegant if I could store both the website name and the physical path in the param file. So, still room for improvement:)

Check now that your new site is up and running:

 

Still a small issue to fix for the https certificate.

The above approach will work perfectly when deploy your packages on the same IIS as where you generated your original Template Website. Normally, webdeploy should be capable of working server-independent.

The problem is that a certificate is bound to a sesrver. So, in case you want to use the above approach for moving a packagae from one server to another, we need a solution for attaching an new certificate to the package when you move to the other server.

I had initially a problem with the certificate related material in the package, webdeploy crashed. It seemed to be related to the fact that I used as password simply a dot. When using a normal password (as above: secret), things worked perfectly.

Conclusion

The above seems to be a lot of work. In fact, most of the things are a one time shot. When deploying a new site, you only need to update an xml file (parameters.xml), tweek the script and run it, e.g. from  the command line (indeed, as admin, you’ll forget) or even better … from your TFS build server. All this can of course be integrated with power shell scripts as well.

… and don’t forget to deploy your LightSwitch app afterwards in the fresh website !