Probably most of You are aware of the app_offline.htm functionality. Basically if you put a html file named app_offline.htm in the root of a web application, the application goes to "offline" mode where all incoming requests are redirected to this file.
This seems great at first sight. You can temporarily disable the application while performing some maintenance. Specifically, you can delete all application files except the app_offline.htm and the application still works (even though the web.config is gone!) As long as the maintenance is completed you just remove or rename the app_offline.htm and the application is back from the void.
There is, however, one caveat and one drawback of this feature.
The caveat is that the app_offline.htm file must be longer that 512 bytes otherwise instead of its content you get ... 404 (can someone explain it?).
The drawback is that the offline mode affects everyone including you! Either anyone or noone can access the application. What would be nice, however, is to have the ability to temporarily redirect requests coming from outside but still be able to use the application locally.
The code below shows how to accomplish that requirement with really simple HTTP handler. To use it you have to:
- compile the source class
- add the handler section to the web.config file and put appoffline.htm (without the _ !) in the root of the application
2: <add verb="*" path="*.aspx" type="AppOffline.AppOfflineHandler, AppOffline" />
1: using System;
2: using System.Collections.Generic;
3: using System.Text;
4: using System.Web;
5: using System.IO;
6: using System.Web.UI;
8: /* Wiktor Zychla, 2007 */
9: namespace AppOffline
11: public class AppOfflineHandler : IHttpHandler
13: string appOffline;
14: /// <summary>
15: /// appoffline.htm file content as singleton
16: /// </summary>
17: public string AppOffline
21: if ( appOffline == null )
22: appOffline = OfflineFileContent;
24: return appOffline;
28: /// <summary>
29: /// Path to the appoffline.htm file
30: /// </summary>
31: public string OfflineFilePath
35: return context.Server.MapPath( "~/appoffline.htm" );
39: /// <summary>
40: /// appoffline.htm file content
41: /// </summary>
42: public string OfflineFileContent
46: return File.ReadAllText( OfflineFilePath );
50: /// <summary>
51: /// Is the application in the offline mode?
53: /// Yes - if the appoffline.htm exists
54: /// </summary>
55: public bool IsOffline
59: return File.Exists( OfflineFilePath );
63: HttpContext context;
65: #region IHttpHandler Members
66: public bool IsReusable
70: return true;
74: public void ProcessRequest( HttpContext context )
76: this.context = context;
78: // offline mode and remote request?
79: if ( !context.Request.IsLocal &&
84: context.Response.Write( AppOffline );
89: // redirect to the default processing pipe
93: context ).ProcessRequest( context );
This approach has its own drawback - it is not "maintenance" aware in a sense that when for example some crucial modules are gone from the /bin subfolder, the application will not load at all because of non-existent modules beeing referenced from within the web.config file.
The nice thing is that you can test the application locally but all remote requests are gracefully redirected to the appoffline.htm. I suggest then to use my feature side by side with the ASP.NET app_offline.htm.
Although a nice alternative, there might be a performance hit in checking a file for existence with every request. I suggest you to use a directory listener instead. You can make a static class that implements this listener and keep it alive in the app domain. You can add a property to the class that checks the state and automatically registers the listener if it doesn't exist yet (to cope with app domain flushes).
However, as you said, your alternative is not really in maintenance state. Remind that using app_ofline.htm also frees all the resources like database and file links. You probably want to have this when you upgrade important libraries...
Thanks for the sharing this website. it is very useful professional knowledge. Great idea you know about company background.
web application development
Post a Comment