Setting up Sitecore with IIS Express

15 September 2011

With the release of Visual Studio 2010 with the IIS Express integration, I started to investigate the usage of IIS Express in combination with Sitecore. In this blog I will discuss the shortcomings with the current implementation in Visual Studio and how I finally implement it.

Why IIS Express

IIS Express is a lightweight, self-contained version of IIS 7.5 optimized for developers. For me, implementing IIS Express has the following benefits:

  • Most new production environments running IIS 7.5, but when your development machine is windows XP (which is very common in large companies) you are bound to IIS 6.0. With IIS Express you can develop in the same IIS version as production. Also it is not possible to run multiple sites in IIS 6.0, so it is hard when your website contains multiple IIS website.
  • It is always a time-consuming task to setup (and keeping it up to date) IIS for all developers. In our situation we have multiple instances of Sitecore running per project (Development, Test branch and Production branch) and sometimes even more if we communicate with webservices applications. IIS Express is also controlled by configuration files, so those could easily be managed with source control so that they are available for al developers.

Visual Studio 2010 SP1

You can use IIS Express is Visual Studio by just right clicking on a webapplication en select ‘Use IIS Express’ and IIS Express will be started for the default project when the solution is run. But what if your solution contains multiple websites (for example a Sitecore site and some web applications with webservices)? Now only the Sitecore site will be started and all other web applications should be started manually…

Also with the Visual Studio 2010 SP1 integration uses the default IIS Express configuration file and we wanted to use a project specific one controlled by source control.

Luckily IIS Express could be started and controlled from the command line, so we are going to build a Visual Studio build task, that will start IIS Express for all websites and uses the configuration file from source control.

Building the Task

Create a new class library project in Visuals studio and create the files shown below:
Building the task

StartIisExpressTask.cs

I think the code is mostly self explaining, but in generally the task checks if the IIS express is running (if so, than starting is not necessary), get the project IIS Express config file and change the paths specified in it, so it match the local developers situation and finaly it starts the IIS Express process for the sites.
public override bool Execute()
{
    // Check if iisexpress process is running;);
    if (Process.GetProcessesByName("iisexpress").Count() != 0)
    {
        // Process is also running, so don't need to start is. If you changed a setting
        // first stop iis express and re-run this program.);
        Log.LogMessage("IIS Express is already running.");
        return true;
    }
  
    // Read the config file;
    var config = File.ReadAllText(string.Format("{0}/applicationhost.config", Path.GetDirectoryName(BuildEngine2.ProjectFileOfTaskNode)));
  
    // Change the [rootpath] tag with the current solution directory;
    config = config.Replace("[rootpath]", Directory.GetParent(BuildEngine2.ProjectFileOfTaskNode).Parent.FullName);
  
    // Write the file again
    File.WriteAllText(string.Format("{0}/_applicationhost.config", Path.GetDirectoryName(BuildEngine2.ProjectFileOfTaskNode)), config);
  
    // Start the main site (sitename should be specified in config file!);
    StartSite("sitecore.dom1.intern");
  
    // Start the services (sitename should be specified in config file!)
    StartSite("services.dom1.intern");
  
    return true;
}
         
/// <summary>
/// Starts the site.
/// </summary>
/// <param name="name">The name.</param>
private void StartSite(string name)
{
    Log.LogMessage("Starting site: " + name);
    var processStartInfo = new ProcessStartInfo(@"C:\Program Files\IIS Express\iisexpress.exe", string.Format(@"/site:{0} /config:""{1}/_applicationhost.config""", name, Path.GetDirectoryName(BuildEngine2.ProjectFileOfTaskNode))) {
        WindowStyle = ProcessWindowStyle.Hidden,
        CreateNoWindow = true,
        UseShellExecute = true
    };
  
    Process.Start(processStartInfo);
}

applicationhost.config

This is a copy of the default IIS Express config file located in the directory: C:\Program Files\IIS Express\AppServer

In this file you can define al your sites and other IIS settings for the project. See my sites-part of the configuration file below:

<!--
*****************************************************************
** Sitecore
*****************************************************************-->
<site name="sitecore.dom1.intern" id="1">
  <application path="/" applicationPool="Clr2IntegratedAppPool">
    <virtualDirectory path="/" physicalPath="[rootpath]\WebSite" />
  </application>
  <bindings>
    <binding protocol="http" bindingInformation="*:80:sitecore.dom1.intern" />
  </bindings>
</site>
  
<!--
*****************************************************************
** Webservices
*****************************************************************-->
<site name="services.dom1.intern" id="2">
         
  <!-- Root -->
  <application path="/" applicationPool="Clr2IntegratedAppPool">
    <virtualDirectory path="/" physicalPath="[rootpath]\webservices" />
  </application>
  
  <!-- Ibis proxy service -->
  <application path="/Ibis.ProxyService" applicationPool="Clr2IntegratedAppPool">
    <virtualDirectory path="/" physicalPath="[rootpath]\webservices\Ibis.ProxyService" />
  </application>
  
  <!-- Aanmeldingen service -->
  <application path="/Aanmeldingen.Service" applicationPool="Clr2IntegratedAppPool">
    <virtualDirectory path="/" physicalPath="[rootpath]\Webservices\Aanmeldingen.Service" />
  </application>
  
  <!-- Activatie service -->
  <application path="/Activatie.Service" applicationPool="Clr2IntegratedAppPool">
    <virtualDirectory path="/" physicalPath="[rootpath]\webservices\Activatie.Service" />
  </application>
  
  <!-- Voorschotten service -->
  <application path="/Voorschotten.Service" applicationPool="Clr2IntegratedAppPool">
    <virtualDirectory path="/" physicalPath="[rootpath]\webservices\Voorschotten.Service" />
  </application>
         
  <bindings>
    <binding protocol="http" bindingInformation="*:80:services.dom1.intern" />
  </bindings>
         
</site>

Note: the [rootpath]-tag will be replaces by the local solution path runtime.

MSBuild.xml

In this file you define the MSBuild task, which will start the task.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">  
  <UsingTask TaskName="StartIisExpressTaks" AssemblyFile="bin/MSBuild.IIS.dll" />
  <Target Name="Build">
    <StartIisExpressTaks />
  </Target>
</Project>

Post-build Event

To run the task, when you build the project you have to add the following post-build event to the project.

Post-build event

Result

When you build your solution now for the first time, IIS Express will be started with all the sites defined in the task.

The result

Final Thoughts

I hoped Visual Studio 2010 SP1 had more buildin options for using IIS Express, but it could easily be solved with the small modifications described in this blog. For me, IIS Express is a real relief, because I’m developing most of my time on Windows XP and with IIS Express I’m not bound to the old IIS 6.0 anymore. Also the source controlled config files are a nice extra and saves a lot of time keeping IIS up-to-date with all the other developers.

Tags: Sitecore , Techniek

terug naar overzicht


Reacties

Dit is wat voor mij!

*
*

Spread the word!