Wednesday, January 15, 2014

Eliminate startup delay in your galleries using Application Initialization

It is well known that ASP.NET applications have a startup delay after they’ve been idle for a while. Every once in a while someone posts in the forum that the gallery is slow to load at first and then is quite fast. That’s because IIS is doing a lot of stuff when that first HTTP request comes in:

  • Spins up an application pool
  • JIT-compiles the code
  • Performs view generation of the EF model
  • Loads HTTP modules
  • Runs initialization code in GSP, which connects to the database and loads application settings and other lookup data.

All that takes a few seconds, which isn’t terrible, but first impressions are important. Wouldn’t it be better if IIS handled this before that first HTTP request comes in?

Well, it can. It’s called Application Initialization and is available in Windows Server 2008 R2 and higher. Once configured, the gallery application is always warmed up and ready to instantly serve HTTP requests, even after periods of inactivity that would normally shut down the application pool. Sweet!

It’s a little tricky to set up, so let’s run through the details.

 

Application Initialization in Windows Server 2008 R2 (IIS 7.5)

In Windows Server 2008 R2, use the Microsoft Web Platform Installer to install Application Initialization. Once installed, configure it by editing applicationHost.config, which is at %WINDIR%\system32\inetsrv\config.

  1. Open applicationHost.config in Notepad, being sure to run it with the "Run as Administrator" option.
  2. Find the <applicationPools> configuration section, and then look for the application pool running your gallery. If you don’t know the pool name, look it up in IIS Manager by right-clicking the application node in the left pane and choosing Manage Application > Advanced Settings. Add startMode="AlwaysRunning" so that it looks like this:

    <add name=".NET v4.5" startMode="AlwaysRunning" managedRuntimeVersion="v4.0" />

  3. Scroll down a little more in applicationHost.config to the <sites> configuration element. Find the entry for the gallery application and add preloadEnabled="true", like this:

    <application path="/gallery" preloadEnabled="true" applicationPool=".NET v4.5">

  4. Restart IIS by executing iisreset in a command prompt running as an administrator.

That’s it. Your gallery is now always running in a warmed up state and ready to instantly serve users. You can test it out by temporarily changing the application pool timeout to one minute, then waiting a couple minutes and browsing to your gallery.

 

Application Initialization in Windows Server 2012 and higher (IIS 8+)

In Windows Server 2012 and higher, Application Initialization is included in the OS but it must be enabled in the Add Roles and Feature Wizard:

app_init

One installed, you can now configure it with IIS Manager – no need to edit applicationHost.config.

  1. Open IIS Manager and navigate to the application pool the gallery is running under. Right click it and choose Advanced Settings. Verify the Start Automatically setting is set to True and the Start Mode is set to AlwaysRunning. Click OK.

    app_init2

  2. In the left pane of IIS Manager, navigate to the gallery web application. Right click the application node and select Manage Application > Advanced Settings. Set Preload Enabled to True.

    app_init3

  3. Restart IIS by executing iisreset in a command prompt running as an administrator.

That’s it. Your gallery is now in an always-on state.

 

Common mistakes

The web is filled with people talking about how they can’t get this feature to work. I’m guessing it’s because they missed one or more of the four critical steps. If you’re having trouble getting things going, make sure you did these steps:

  1. Installed Application Initialization (either through Web PI for IIS 7.5 or through the Add Roles and Features Wizard for IIS 8+).
  2. Application pool has Start Automatically = True.
  3. Application pool has Start Mode = AlwaysRunning.
  4. Application has Preload Enabled = True.