23

I think I heard that ASP.NET applications will shut down after a while of being idle (i.e. no visitors).

Is there a way to prevent this behavior from happening? I have a timer that runs some code from the global.asax.cs application_start event, and want to make sure that it continues to run, even when no visitors are hitting the site.

Thanks in advance!

John B
  • 20,062
  • 35
  • 120
  • 170
  • 1
    Depending on the code that runs, I would make that a Windows Service that runs on the server. – Martin Aug 20 '09 at 13:59
  • 1
    Ideally yes, this code should be in a service. However, for flexibility, and portability we decided to have the code run from the global.asax.cs. – John B Aug 20 '09 at 14:03

4 Answers4

31

You can do it a few ways.

  1. If you have control over IIS, you can change the "Idle Timeout" within the application pool, by default it is 20 minutes.
  2. 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.

Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173
  • +1 for the external keep alive, and another +1 (if I could) for recommending moving the timer code into a service. – Jon Tackabury Aug 20 '09 at 14:04
  • 1
    +1 External keeep alive works well. You could even write your own service to run off a server to keep the site alive. – Audioillity Aug 20 '09 at 14:11
  • 1
    Set the "Idle Timeout" to zero to disable recycling altogether. See also here: https://stackoverflow.com/questions/1596267/how-to-disable-the-application-pool-idle-time-out-in-iis7 – Stefan Steiger Sep 14 '16 at 09:25
  • I've tried setting the 'Idle Timeout' to zero but that still caused my application to be shutdown after a day or so. – Thomas Luijken May 04 '17 at 07:48
  • @ThomasLuijken eventually many hosts also have a set recycle time, 1440 minutes I believe is the default. – Mitchel Sellers May 08 '17 at 16:21
  • @MitchelSellers, you are correct! I figured this out the same day you posted the comment. My app has been running ever since. Thanks for the addition! – Thomas Luijken May 12 '17 at 10:54
19

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;
            }
        }
    }
Eduardo
  • 5,645
  • 4
  • 49
  • 57
  • Ingenious in theory ! But has anybody tried it ? Does it work ? Also, if you do this externally, and do this every 21 minutes after app_start, then it will recycle, and then restart. Write the last startup time and shutdown time to database, then you can restart it with a windows service after every recycle completed. – Stefan Steiger Sep 14 '16 at 09:45
  • 1
    I tested before posting – Eduardo Oct 04 '16 at 14:40
  • U would never want to do this in production if it will recycle the application pool a user could loose their data?. Esp if coming from a mobile front end scneario – c-sharp-and-swiftui-devni Apr 16 '22 at 20:26
7

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.

0

Go to your task manager and kill w3wp.exe (or aspnet_wp). Nothing can stop you from doing this. Bottom line, ASP.NET applications cannot assume continuous functionality.

However, you can launch a separate thread, which will not be subject to idle timeouts -- as long as it has something to do. I've used this technique to control a 45-minute import that would continue whether or not anyone was on the web site. I used a transaction log and Application_Start so that when the process was recycled, it would pick up where it left off.

harpo
  • 41,820
  • 13
  • 96
  • 131