While Sandrino's solution might work... here is a solution that does not require the web role to run in elevated security mode, and also will force the application to start when the webrole starts (before the first user visits the site). This solution will also work on older versions of IIS/Windows Server that does not require IIS 8's "Application Initialization" feature.
Just add a WebRole.cs with the following content:
using System;
using System.Net;
using System.Net.Security;
using System.Threading;
using Microsoft.WindowsAzure.ServiceRuntime;
namespace Website
{
public class WebRole : RoleEntryPoint
{
public override bool OnStart()
{
WarmUpWebsite("HttpIn");
return base.OnStart();
}
public override void Run()
{
while (true)
{
WarmUpWebsite("HttpIn");
Thread.Sleep(TimeSpan.FromMinutes(1));
}
}
public void WarmUpWebsite(string endpointName)
{
// Disable check for valid certificate. On som sites live HTTP request are redirected to HTTPS endpoint. And when running on staging SSL the certificate is invalid.
RemoteCertificateValidationCallback allowAllCertificatesCallback = (sender, certificate, chain, sslPolicyErrors) => true;
ServicePointManager.ServerCertificateValidationCallback += allowAllCertificatesCallback;
try
{
RoleInstanceEndpoint endpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[endpointName];
string address = String.Format("{0}://{1}:{2}", endpoint.Protocol, endpoint.IPEndpoint.Address, endpoint.IPEndpoint.Port);
//This will cause Application_Start in global.asax to run
new WebClient().DownloadString(address);
}
catch (Exception)
{
// intentionally swallow all exceptions here.
}
ServicePointManager.ServerCertificateValidationCallback -= allowAllCertificatesCallback;
}
}
}
Credits goes to: http://weblogs.thinktecture.com/cweyer/2011/01/poor-mans-approach-to-application-pool-warm-up-for-iis-in-a-windows-azure-web-role.html
The while(true) could be replaced with Sandrino's approach or you could disable application pool idle timeout: http://blog.smarx.com/posts/controlling-application-pool-idle-timeouts-in-windows-azure