Possible fix when ‘Always On’ Azure web site is sometimes slow to load
Overview
Asp.Net websites load slowly for the first visit due to JIT compilation of assemblies. I’ve experienced 30–60 second wait times for this but subsequent visits are much faster.
There is a setting in the Azure portal that makes a website ‘Always On’:
Without this, the application pool releases it’s resources for the website after 30 minutes of inactivity. Every time this happens, the next visit will suffer the same slow load time.
In the past I’ve used services like StatusCake to visit the site every 15 minutes to prevent it from going idle.
The Problem
I have a website (using .Net Framework 4.6) that has this setting turned on, yet still suffers the same symptoms when it hasn’t been hit for a while. The site forces https by using a url rewrite rule in web.config:
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to https">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="Off"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/>
</rule>
</rules>
</rewrite>
</system.webServer>
The problem is that this rule prevents Azure from sending the ‘Always On’ ping request to your application.
The Fix
You need to add another rule just above the https rule like this:
<system.webServer>
<rewrite>
<rules>
<rule name="No HTTPS for user agent AlwaysOn and Root of site" stopProcessing="true">
<match url="^$"/>
<conditions>
<add input="{HTTP_USER_AGENT}" pattern="^AlwaysOn$" />
</conditions>
<action type="None" />
</rule>
<rule name="Redirect to https">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="Off"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/>
</rule>
</rules>
</rewrite>
</system.webServer>
This will allow Azure to prevent the site from going idle without the use of any monitoring software.