Application Initialization for Azure Service Apps (and Sitecore)

We have been having a problem with Sitecore in Azure PAAS – it appears that when auto-scaling scales out, App Services are being put into rotation before they have started up. This is causing all sorts of weirdness.

Sitecore support recommended making sure that we had Application Initialization configured. That seems a good idea. I’m not sure why the guys who set up this instance didn’t; perhaps they were unaware of it (and to be fair, it’s something I’ve not looked at before).

How Application Initialization works:- you add a section to your web.config:

<applicationInitialization doAppInitAfterRestart="true" skipManagedModules="false">
<add initializationPage="/?ServerWarmUp=example" hostName=""/>
<add initializationPage="/?ServerWarmUp=example2" hostName=""/>

This tells IIS, after restart, to call those two pages to warm up the instance. There’s a few bits to unpack here, though.

The initializationPage is the page(s) you want to load to warm up the instance. In my case, I’m using home pages. The Query String is because I’ve read that the page path must be unique – but we want to hit the home page for multiple sitecore sites. The query string could be anything, as long as they’re all different.

hostName is not the domain you’re calling – it’ll always be the local IIS – but rather that’s the Host Header that will be used. That’s useful for us; it lets us hit multiple sites. I think, anyway.

Now, Azure apparently has a neat feature; it’ll run Application Initialization and hold an app service out of rotation until it has received a response from all of those pages. Note, that’s any response – 500 Server Errors would qualify.

There is another problem – Application Initialization only uses HTTP. Therefore, you’ll need a redirect rule in IIS ahead of your HTTPS redirect that allows the Application Initialization Warmup Agent to make HTTP requests without being redirected:

<rule name="No redirect on warmup request (initialization user agent)" stopProcessing="true" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)">
<match url=".*" />
<add input="{HTTP_USER_AGENT}" pattern="Initialization" />
<action type="Rewrite" url="{URL}" />

Make sure this goes ahead of your HTTPS Redirect rule.

You can get this running on a local machine, but you’ll need to make sure the Feature is installed for IIS, and I had to manually edit the applicationHost.config.

Once you’ve done all that, do an IIS Reset; your site should be automatically loaded. You should not have to make a request to it to get it to load.

Application Initialization can also remap requests to a static page (“Loading, please try again in a moment), but as the whole point of this is to hold an App Service until it has warmed up, we should not need this.

Should you want to do things like test the Redirect rule, you’ll want to change your user-agent. Don’t forget to clear your HSTS cache (chrome://net-internals/#hsts), and browser’s file cache.

Other Resources:

Application Initialization for Azure Service Apps (and Sitecore)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.