The forgotten file for .Net Web Sites and Applications: Now to throttle a website

I frequently observe that may sites and applications do not have a Global.asax file. One sweet use of such a file is the ability to keep a site appearing well behaved to users and the ability to throttle load the moment any component starts to fail due to load, network issues or whatever.

  • First, you NEVER want a site to vomit .Net/C# error messages. Not only is it not professional, the information shown can sometime be used to hack the site.
  • Second, if components are failing due to load or unexpected problems, you really want to tell the users to go away and come back later. The problem is knowing when to send users away.

First we add a global error trap – if you forgot to do a try/catch somewhere in your code – fear not, this will handle it!

   1: /// Page to send user to if any unexpected error occurs
   2: private static string offlinepage = "~/NotAvailable.htm";
   3: /// Cookie Name
   4: private static string offline = "_Offline_";   
   5: void Application_Error(object sender, EventArgs e)
   6: {
   7:     if (String.Compare(Request.AppRelativeCurrentExecutionFilePath, offlinepage, true) == 0)
   8:     {
   9:         return;
  10:     }
  11:     // Production/QA Logic
  12:     DateTime offlineuntil = DateTime.Now.AddMinutes(15);
  13:     HttpCookie cookie = new HttpCookie(offline, offlineuntil.ToString());
  14:     cookie.Expires = offlineuntil;
  15:     Response.Cookies.Add(cookie);
  16:     Response.Redirect(offlinepage, true);
  17: }

So when an error occurs, the user will be redirected to the offlinepage AND a cookie added that will last for 15 minutes.

 

Now we use another event to make sure that any attempt for this user to connect again is gracefully and politely refused.

   1: void Application_AuthorizeRequest(object sender, EventArgs e)
   2: {
   3:     DateTime now = DateTime.Now;
   4:     if (!string.IsNullOrEmpty(Request.Cookies[offline].Value)
   5:         && DateTime.TryParse(Request.Cookies[offline].Value, out now)
   6:         && now > DateTime.Now)
   7:     {
   8:         if (String.Compare(Request.AppRelativeCurrentExecutionFilePath, offlinepage, true) == 0)
   9:         {
  10:             return;
  11:         }
  12:         Response.Redirect(offlinepage, true);
  13:     }
  14: }

We actually overdid the testing by not only checking for the cookie (which should have expired), but checking the datetime if the cookie is present (incase something goes odd with the browser handling of cookies).

 

So what happens? Users are denied access on the first error. If a component is timing out – that’s fine, the load will be quickly trimmed to the volume that can be handled without error. No crashing servers. No ugly error messages. A well behaved website!

 

P.S. The offline page should be a html page (unless localization is an issue) so it has minimal load on the IIS Server.

Comments

  1. I like how I can tell who the behind-the-scenes author is by the use of the word "sweet." This is a great, useful article - the global.asax file is not being used by apps in my new dev environment and this will be immediately useful. Thanks! -Syd

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple WP7 Mango App for Background Tasks, Toast, and Tiles: Code Explanation

Yet once more into the breech (of altered programming logic)

Error : /ScriptResource.axd : Invalid viewstate.