LightSwitch IIS Deployment Enhancements in Visual Studio 2012

Note: This information applies to LightSwitch in Visual Studio 2012 ONLY (LightSwitch V2). This post was updated on 6/8/2012 to include changes in RC. For information on deploying LightSwitch applications built with Visual Studio 2010 (LightSwitch V1), please see: Deployment Guide: How to Configure a Web Server to Host LightSwitch Applications

In my last post on deployment I mentioned I’d point out some LightSwitch deployment goodies you get in Visual Studio 2012. There are some really nice improvements and it’s important to point out some of the changes that you see when you deploy your LightSwitch V2 applications to your own web servers. This post will focus on IIS deployment only. Note however that I have the latest Windows Azure SDK for .NET installed. You should install this to get a better Azure publishing experience but it also upgrades the LightSwitch publish wizard which I will be showing in the screenshots below.

For information on Azure deployment enhancements please see: Publishing LightSwitch Apps to Azure with Visual Studio 2012

Setting up IIS

For the most part, deployment is the same as it was before, albeit with a few nice new features I’ll discuss below. So you can set up your web and SQL servers the same as before. For information on setting up your web servers see:

For IIS7: Deployment Guide: How to Configure a Web Server to Host LightSwitch Applications

For IIS6: Deploying LightSwitch Applications to IIS6 & Automating Deployment Packages

What’s really nice is that you can deploy LightSwitch V2 applications (built with Visual Studio 2012) to any web server you set up using the V1 LightSwitch 2011 Server Runtime prerequisites. However, you no longer need the LightSwitch server prerequisites or any LightSwitch specific tools. The same Web Platform Installer feed used by the rest of Visual Studio web applications will also work for LightSwitch applications which uses Web Deploy 3. This reduces the numbers of steps to provision a server and should make deployment a lot more predictable. Read on to see what else we improved… 

Service-Only Publish

The first new feature you will notice on the publishing wizard is the option to publish just the service and not the client. With LightSwitch now supporting OData, a standard web-based protocol for accessing data via services, you can use LightSwitch to easily create smart, secure, middle-tier data services that can be called from a variety of clients. If you choose this option, the LightSwitch client will not be published.

image

Import Settings

Another new feature you will notice on the publishing wizard is a button which allows you to import a .PublishSettings file to configure the deployment. Webmatrix and Web Application projects in Visual Studio also have this feature. Azure Web Sites and some ISPs/hosters will give you this file to make it easy to deploy to their systems. For more information on creating these files yourself see: IIS.NET: Generate a Profile XML File.

image

Specify Security Settings

Next is the option to set up security settings. Note the set of tabs you now get to specify all the security options in one place. The second tab is new and allows you to set up HTTPS. By default, communication between the client and the server uses the HTTP protocol instead of the HTTPS protocol, which is more secure. Secure Sockets Layer (SSL) encryption helps to protect confidential or personal information that’s sent between a client application and a server. We recommend that you configure SSL for any site that hosts an application that’s based on LightSwitch so that data on the wire is encrypted. For more information, see Configuring Secure Sockets Layer in IIS 7.

image

Your Choice: Publish Database Schema

Another new feature is the ability to choose whether you want to publish the database schema when you do a direct deployment (on the Publish Output step you select “Remotely publish to server now”). When you configure your database connections you will now see a checkbox that indicates whether LightSwitch should attempt to update the schema too. So if you are updating your application but you didn’t make any schema changes in the data designer, then you can uncheck this box to speed up the deployment. However, be careful – if you are unsure leave this box checked.  

 image

Additionally, if you are generating a package on disk (on the Publish Output step you select “Generate a package on disk”) you can also choose whether you want to generate the SQL database script as part of the package. Also if you are using any external data sources then additional tab will allow you to configure the external connection strings.

image

Generating and Installing Deployment Packages

When you choose to create a deployment package on disk instead of direct deployment, a package .ZIP file is created for you that you can import into IIS. In IIS7 you can import this package using IIS Manager, right-clicking on the Default Web Site and then selecting Deploy –> Import Application as detailed in the deployment guide. This process will prompt you for all the required parameters to install the package and set up the database. However in IIS6 you need to use the command line to import these, as I showed in my last deployment post.

In LightSwitch V2 there are now also a set of files that you can use to automate the importing of the package and specifying parameters via the command line. These are starting points and should be modified as necessary depending on your settings. Administrators can use these to get the right settings together quickly instead of having to manually create these files.

image

If you open up the readme it will detail the prerequisites and how to use the batch file. It is similar to the settings I detailed in the previous deployment post (which only dealt with V1 applications). Here are the V2 specific settings in the .SetParameters.xml. (Note that there is a Beta bug where you will see three additional internal parameters, “dbFullSql_Path”, “Update web.config connection string”, and “LightSwitchApplication_Path” so you will need to remove these lines if you see them).

Modify the following settings in bold to your specific values: 

 <?xml version="1.0" encoding="utf-8"?>
<parameters>
  <setParameter name="DatabaseAdministratorConnectionString" 
                value="Data Source=servername\SQLEXPRESS;Initial Catalog=Application1;<br>                       User ID=admin;Password=adminPassword" />
  <setParameter name="DatabaseServer" value="servername\SQLEXPRESS" />
  <setParameter name="DatabaseName" value="Application1" />
  <setParameter name="DatabaseUserName" value="dbuser" />
  <setParameter name="DatabaseUserPassword" value="dbpassword" />
  <setParameter name="Application1_IisWebApplication" value="Default Web Site/Application1" />
 </parameters>

Note that the DatabaseAdministratorConnectionString is only used to create/update the database. The DatabaseUserName and DatabaseUserPassword values are used to construct the connection string in the web.config and is the credential used by the LightSwitch middle-tier when your app is running.

If your application is using Windows authentication you will need to specify one additional parameter:

   <setParameter name="Application Administrator User Name" value="DOMAIN\UserName" />

If your application is using Forms authentication you will need to specify three additional parameters:

   <setParameter name="Application Administrator User Name" value="UserName" />
  <setParameter name="Application Administrator Full Name" value="Full Name" />
  <setParameter name="Application Administrator Password" value="strongPassword" />

Once you configure the .SetParameters.xml file, copy the .zip, .cmd and .xml files to the web server where you are the administrator and run the .Deploy.cmd. To install the package and the database run:

>Application1.Deploy.cmd /Y

See the readme for details on more options.

Setting up the System Administrator User

In LightSwitch V1 when you select to use either Forms or Windows authentication, the deployment wizard would ask for the credentials of the System Administrator that should be in the database initially and then after it deployed the database it would call Microsoft.LightSwitch.SecurityAdmin.exe (which is installed with the runtime prerequisites) to insert the corresponding records into the database at that time.

Now the System Administrator credentials you enter in the wizard are stamped in web.config. So now when your application starts the first time, LightSwitch looks to see if that user exists in the database and if it doesn’t, it will be created. This strategy works even when the LightSwitch runtime prerequisites are not installed. So now if you ever need to “reset” the admin password, you can modify web.config – either directly or via re-publish. If you look in the published web.config you will see these new keys:

     <!-- If no admin user exists, create an admin user with this user name -->
    <add key="Microsoft.LightSwitch.Admin.UserName" value="UserName" />
    <!-- When creating an admin user with Forms auth, use this as the full name -->
    <add key="Microsoft.LightSwitch.Admin.FullName" value="Full Name" />
    <!-- When creating an admin user with Forms auth, use this as the pasword -->
    <add key="Microsoft.LightSwitch.Admin.Password" value="strongPassword" />

Once your application runs for the first time and the System Administrator is added, you can remove these settings to avoid storing the password in your web.config indefinitely.

Wrap Up

Most of these are the more advanced IIS deployment settings that should help IT administrators with hosting/deploying LightSwitch applications. And it’s definitely nice that you can host V2 applications without changing your V1 servers. Most users will not need to wade through these settings and will instead deploy directly, hand off a package to an administrator to install, or use the user interface in IIS Manager to import the package manually. However, if you want the power of automated install of packages, and more control over your deployments then these new features should help.

Enjoy!