Click here to Skip to main content
15,879,326 members
Articles / Web Development / IIS

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

Rate me:
Please Sign up or sign in to vote.
4.82/5 (8 votes)
19 Aug 2009CC (ASA 2.5)2 min read 74.5K   38   18
IIS can be a bully to your poor web application and when you're in a hosted environment you have even less control. In this post we discuss how we can keep your website from being recycled and do it without needing external code!

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) {
        HttpContext.Current.Cache.Remove("Refresh");
        remove.EndInvoke(null);
    }

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

    //add this job to the cache
    HttpContext.Current.Cache.Add(
        "Refresh",
        work,
        null,
        Cache.NoAbsoluteExpiration,
        Cache.NoSlidingExpiration,
        CacheItemPriority.Normal,
        (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("http://www.website.com/", string.Empty);
}
catch (Exception ex) {
    //snip...
}
finally {
    refresh.Dispose();
}

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.

This article was originally posted at http://somewebguy.wordpress.com?p=532

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
BugNull Reference exception Pin
xebedy26-Nov-13 1:13
xebedy26-Nov-13 1:13 
GeneralMy vote of 5 Pin
Kevin-Brunner31-Dec-12 4:16
professionalKevin-Brunner31-Dec-12 4:16 
QuestionCan't you just do this in IIS for the application pool? Pin
User 333507321-Oct-12 22:29
User 333507321-Oct-12 22:29 
AnswerRe: Can't you just do this in IIS for the application pool? Pin
Rapsy Tree9-Sep-14 10:17
Rapsy Tree9-Sep-14 10:17 
GeneralMy vote of 5 Pin
Maziar Taheri7-Oct-12 21:49
Maziar Taheri7-Oct-12 21:49 
GeneralKeep it alive Pin
Larry BS5-Jun-11 2:56
Larry BS5-Jun-11 2:56 
GeneralRe: Keep it alive Pin
gilbi9-May-13 18:58
gilbi9-May-13 18:58 
GeneralRe: Keep it alive Pin
Member 197108410-Aug-18 8:39
Member 197108410-Aug-18 8:39 
GeneralError Pin
manofatlantic5-Nov-10 14:37
manofatlantic5-Nov-10 14:37 
GeneralNeat Idea Pin
AspDotNetDev2-Sep-09 20:05
protectorAspDotNetDev2-Sep-09 20:05 
GeneralRe: Neat Idea Pin
webdev_hb3-Sep-09 1:30
webdev_hb3-Sep-09 1:30 
GeneralRe: Neat Idea Pin
AspDotNetDev3-Sep-09 10:46
protectorAspDotNetDev3-Sep-09 10:46 
GeneralRe: Neat Idea Pin
webdev_hb3-Sep-09 12:07
webdev_hb3-Sep-09 12:07 
GeneralRe: Neat Idea Pin
AspDotNetDev3-Sep-09 12:23
protectorAspDotNetDev3-Sep-09 12:23 
GeneralRe: Neat Idea Pin
webdev_hb3-Sep-09 12:43
webdev_hb3-Sep-09 12:43 
GeneralRe: Neat Idea Pin
SamNaseri15-Nov-11 18:52
SamNaseri15-Nov-11 18:52 
Questionare you sure it works? Pin
mintxelas18-Aug-09 23:51
mintxelas18-Aug-09 23:51 
AnswerRe: are you sure it works? Pin
webdev_hb19-Aug-09 2:16
webdev_hb19-Aug-09 2:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.