Can you prevent your ASP.NET application from shutting down?
You can do it a few ways.
- If you have control over IIS, you can change the "Idle Timeout" within the application pool, by default it is 20 minutes.
- If you do NOT have control over IIS (Shared or other hosting), you can use an external service to ping the site. MyWebKeepAlive or Pingdom are good options for this.
If you are under option two, I strongly recommend an external source, as it is less to manage, and if you don't have IIS available to configure, you most likely don't have the server available to add a ping application to.
In addition, I do not recommend something that sits in-process with the web application, as I have found that those can cause issues when the application really does need to recycle...
One last thought
If you do go the route of modifying the IIS Idle timeout, I do recommend setting a regular recycle of the application pool. As many web applications do benefit from a periodic recycle, and most commonly the idle timeout is the biggest regular recycle factor.
Besides external keep alive you can make an internal keep alive inside global.asax
:
static Thread keepAliveThread = new Thread(KeepAlive);
protected void Application_Start()
{
keepAliveThread.Start();
}
protected void Application_End()
{
keepAliveThread.Abort();
}
static void KeepAlive()
{
while (true)
{
WebRequest req = WebRequest.Create("http://www.mywebsite.com/DummyPage.aspx");
req.GetResponse();
try
{
Thread.Sleep(60000);
}
catch (ThreadAbortException)
{
break;
}
}
}
In IIS 6, go to the Application Pools section, and right-click > Properties on the pool which hosts the ASP.NET application in question. Go to the Performance tab and uncheck "Shutdown worker processes after being idle for:"
In IIS 7, go to the Connections pane and find Application Pools, and select Advanced Settings for the pool which hosts your application. Find the "Idle Timeout" property and set it to "0" (this disables it).
The default is 20 minutes of inactivity. By unchecking the box, once your AppDomain is loaded by the worker process, it will never die (unless you kill the process or something of course). By default, IIS will recycle the process when it reaches some limit, such as a memory cap, but it will also start a new one and "phase over" all incoming requests until the old one is unused, so as to minimize disruption.
If you do not have direct control over your IIS configuration (shared host, for example) your best bet is to have a small application running on a separate system - say, an always-on workstation - which hits your site every x minutes to keep the application pool from timing out. Nothing fancy - a simple WebRequest and a while() loop in a console application will do.