Thursday 3 April 2014

Automatically Deploying IIS Express with your Web-based Software

Why IIS Express?
While a lot of businesses use Microsoft’s IIS to host their websites, a Microsoft Server operating system with a full IIS suite is not always necessary, and sometimes too complicated for the average user to configure.

Microsoft provides a free alternative – IIS Express – which provides the user with basic web hosting capabilities. However, automating the installation and configuration of IIS Express is not as easy as it could be. The process is explained in detail here.

Context
At Digiata Technologies, we needed to simplify the installation and setup process for our software package, Stadium 5.

Stadium 5 requires a web server for users to deploy applications they create. Previous versions of Stadium assumed the user had IIS installed and configured. We needed to remove the dependency, and therefore install IIS Express if the user didn’t have IIS installed. Not all users would be installing on Microsoft Server 2012 or similar operating systems. Similarly, not all users would have IIS installed on their operating systems – so we needed to automatically install IIS Express, should the user not have IIS.

The ideal result would be to completely protect users from any IIS or IIS Express installation or configuration steps, with our software acting as a layer between IIS Express and the user-created web applications.

Benefits of IIS Express
IIS Express 8.0 is the relatively new, lightweight and free brother to IIS. You would consider using it if:
·         You don’t want to pay for a Server package with IIS
·         Your software needs to deploy in an environment that doesn’t necessarily have IIS installed, and you don’t want the users to have to install a web server themselves
·         You don’t need all the features of IIS
·         You need Microsoft support for your product

Overall IIS structure and functionality
IIS Express is not entirely intuitive. It is quite different to IIS, and does not come with a Graphical User Interface (GUI) like IIS, but rather relies purely on .config file manipulation.

·         The installation path is defaulted to your Program Files folder
·         As each user runs the iisexpress.exe process, located in the installation path, IIS Express creates a home directory for that user in their Documents folder. Configuration files for that instance of IIS Express are stored in that folder
·         The iisexpress.exe process runs as the NT AUTHORITY\SYSTEM user. Should you access databases from your web application, it will use that user to access the database.
·         Each user’s applicationHost.config file in his home directory stores that user’s web application information in XML format. As a result, each user will have his own web application list

In some cases, such as ours, it is not desirable to have different instances of IIS Express for different users. Unfortunately, it’s not very simple to run one instance of IIS Express between multiple users:

Automatically installing IIS Express
We used the WIX Toolset to build our installer file. The installer downloads and installs IIS Express if no IIS is installed. This functionality could easily be replicated with other install kits.






Running IIS Express between multiple users
When you run iisexpress.exe from the command line, you can specify the flag /userhome:[home], where [home] can point to any directory. Using this means you can have a central IIS Express configuration file in a location such as your C:\Users\Public\Documents folder.  Beware of choosing a folder that some users don’t have access to.

The first time this command is run, the configuration files will be created in the userhome directory if they do not yet exist.

However, when setting a common userhome, we encountered a problem with using the /userhome flag:

IIS Express could no longer locate the aspnet.config file, because %IIS_USER_HOME% variable that IIS Express uses became invalid.  This caused errors when trying to run a web service from IIS Express.

No solution could be found to the problem apart from programmatically hard coding the applicationHost.config file in the userhome directory to point to the aspnet.config file. This involved simply searching for instances of %IIS_USER_HOME% and replacing them with the actual directory location. This was achieved by passing through the XML file to this C# method:







Programmatically adding an application to IIS Express
IIS Express applications are all defined in the relevant applicationHost.config file (in the userhome directory – either the default one or the one you manually specified.)

Adding an application is as simple as adding a site node to the sites node in the config file. The physicalPath attribute points to the path containing the web application’s files.


Programmatically launching IIS Express applications
Whether using C# or any other language to launch IIS Express, the principles remain the same.

Simply run iisexpress.exe from its installation directory with two flags:
-          /userhome:[userhome], filling in the userhome directory you’ve chosen
-          /site:[site], filling in the site name you chose for this web application

This does the following:
-          Launches IIS Express, if it is not already running
-          Creates the userhome directory with the required configuration files, if it has not yet been created
-          Attempts to open the specified site. This will output an error to the console if the site does not exist.

Conclusion
With a few minor hiccups, we managed to create a system to automatically deploy and use IIS Express to host our web applications.

Through IIS Express’s XML configuration file, programmatic manipulation of IIS Express is simple and enables you to take control of IIS Express for the user, simplifying deployment of any applications you have that need to be hosted on the user’s computer.

Although IIS Express relatively new and free to use, it is most definitely suitable for basic business functionality. If your clients don’t need to get their hands dirty with their web server software, it might be a viable option to go with IIS Express and programmatically deal with the configuration yourself.

No comments:

Post a Comment