The product of a web developer with a little too much caffeine

Keep Your Website Alive (Don’t Let IIS Recycle Your Website)!

with 2 comments

Have you ever opened a page for one of your websites and it lags for awhile before it finally shows a page but then all of your following requests are quick? If you were to look up the problem you’d find that often it ends up having to do with IIS meeting an idle time limit and shuts down your site. There is even some software you can purchase to fix the problem for you.

But who wants to spend money on something like that? Especially when we can solve this ourselves — even for Hosted Environments!

Stayin’ Alive — (ack! bad pun again!)

If you happened to check out that software above then you can probably glean what it does just from the title. I’d rather not devote my personal machine to something like that so lets see if we can’t approach this from an alternative route.

private static void _SetupRefreshJob() {

    //remove a previous job
    Action remove = HttpContext.Current.Cache["Refresh"] as Action;
    if (remove is Action) {

    //get the worker
    Action work = () => {
        while (true) {
            //TODO: Refresh Code (Explained in a moment)
    work.BeginInvoke(null, null);

    //add this job to the cache
        (s, o, r) => { _SetupRefreshJob(); }

If we place this bit of code in the Global.asax and call it when Application_Start() is raised, we can basically start a job that keeps our website alive. You could just as easily use a Thread to host the refresh method but for this example we simply used an Action delegate (but if you are using an earlier version of .NET then you might HAVE to use a Thread to do this).

Once our application starts the refresh job is also started and is saved to the cache. In this example we’re using 60 seconds, but you can change this to be as often as you like.

So How Can We Keep It Fresh?

So how about an example of some code we can use? Here is a simple example that could keep our website alive. Replace the //TODO: in the example above with something like the following.

WebClient refresh = new WebClient();
try {
    refresh.UploadString("", string.Empty);
catch (Exception ex) {
finally {

This snippet uses a WebClient to actually make an HTTP call to our website, thus keeping the site alive! We could do any number of things from this code like updating local data or get information from external resource. This can be used to keep our site alive and our content refreshed, even if we’re using a Hosted Environment!

It is worth nothing that might not actually need to do an HTTP call back to your website. It is possible that using any method will keep your website from being killed off (but I haven’t tested it yet so let me know what happens if you try it). This example, however, has been tested and works quite well with my provider.


Written by hugoware

August 19, 2009 at 2:34 am

2 Responses

Subscribe to comments with RSS.

  1. I’ve been using Pingdom’s free account to monitor my site every 60 seconds and so never noticed this issue. This does sound like a clever fix to a problem that should not have ever seen the light of day.



    August 25, 2009 at 4:25 pm

    • I agree, I remember back in the day when I first got started with ASP.NET I used to struggle with this a lot. Its funny that only now, many years later, did I think about this as a solution – up until now I had to use a desktop/server application to do an occasional request to keep things alive.


      August 25, 2009 at 4:33 pm

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: