Miles Chen Miles Chen - 3 months ago 15
ASP.NET (C#) Question

IIS turned off my application automatically?

I just noticed that IIS turned off my ASP.NET web application automatically after it idled 20 minutes. The website is hosted on Godaddy.com.

Below is the Global class which logs the Application_Start and Application_End methods. And the image I uploaded is the result that I saw.

It turned off at 2013-05-24 06:42:40 after the last call I did at 06:22:01. (20 minutes, hua...)

After one day, I did another call at 2013-05-25 03:05:27, the website was awakened.

Strange? I didn't want my website to sleep. Is there any way to keep it sane all the time?

public class Global : HttpApplication
{
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
log.Debug("Application_AuthenticateRequest -> " + base.Context.Request.Url);
}

protected void Application_End(object sender, EventArgs e)
{
log.Info("Application_End");
}

protected void Application_Start(object sender, EventArgs e)
{
log.Info("Application_Start");
}
}


IIS turned off my application automatically?

Answer

The IIS/asp.net is turn off the application if you do not have any request for some time to save resource.

Now this usually handle on server side if you wish to avoid it, but in your case you can't interfere with the IIS setup, so one trick is to create a timer that reads every 10 minutes or so one page.

You start it when application starts, and do not forget to stop it when application is go off.

// use this timer (no other)
using System.Timers;

// declare it somewhere static
private static Timer oTimer = null;

protected void Application_Start(object sender, EventArgs e)
{
    log.Info("Application_Start");

    // start it when application start (only one time)
    if (oTimer == null)
    {
        oTimer = new Timer();

        oTimer.Interval = 14 * 60 * 1000; // 14 minutes
        oTimer.Elapsed += new ElapsedEventHandler(MyThreadFun);
        oTimer.Start();
    }   
}

protected void Application_End(object sender, EventArgs e)
{
    log.Info("Application_End");

    // stop it when application go off
    if (oTimer != null)
    {
        oTimer.Stop();

        oTimer.Dispose();
        oTimer = null;
    }   
}


private static void MyThreadFun(object sender, ElapsedEventArgs e)
{
    // just read one page
    using (System.Net.WebClient client = new System.Net.WebClient())
    {
        client.DownloadData(new Uri("http://www.yoururl.com/default.aspx"));
    }
}

One note, do not use this trick unless you needed because you create one more thread living for ever. Usually google reads the web pages and keep it "warm", the auto turn off usually occurs if you have a very new site that google and other search engines did not start to index, or if you have one two page only that never change.

So I do not recomended to do it - and is not bad to close your site and save resource, and your site is clear from any forgotten open memory and start fresh...

Similar Articles.

Keep your ASP.Net websites warm and fast 24/7
Application Initialization Module for IIS 7.5
Auto-Start ASP.NET Applications (VS 2010 and .NET 4.0 Series)