This post to describe basic steps to write HttpModule and how it rescued mission critical application from not hitting the dead line.
HttpModule is the mechanism that facilitates implementing cross cutting logic for incoming ASP.NET requests. ASP.NET uses it extensively under the covers for its needs too – Session management, Authentication to mention a few. What I love most with HttpModules is that there is no need to alter the application itself – just implement IHttpModule interface and tweak web.config. That is all.
Steps to build HttpModule
- Step 1 Create Class Library
- Step 2 Implement IHttpModule Interface
- Step 3 Configure ASP.NET application to use HttpModule
- Step 4 Test the application
Next section describes each step in detail.
Here I implement any logic I want to run before the page got hit. HttpModule serves as a filter or gatekeeper for incoming Http requests. Following case study explains how such approach saved mission critical application.
Critical mission application was to go live in few days. The schedule was tight. All functional and integration tests went well. The last one was load and stress tests. During the the load test turned out that one of the network components was crashing as a result of memory leak. The component was sending http headers. We decided to configure the component to send the data in Query Strings instead Http Headers [making story short…]. We needed to find the way to teach the application to look the data in Query Strings instead Http Headers. HttpModule to the rescue!! We developed simple HttpModule that was hijacking the request, scrapping the Query String and setting the data inside Http Headers, where the application was looking for it. Heaven. Worked like magic.
Adding Http Headers in .Net is not straight forward since the collection is read only. Reflection to the rescue! Following code explains how to hack Http Headers collection to make it writable – http://forums.asp.net/p/979853/1250479.aspx#1250479. Then we needed to drop the extra Query String before it got to the application. My search landed directly to ScottGu’s RewriteUrl post – http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx. What’s left is make reflection run faster by caching type’s constructor as described here – http://weblogs.asp.net/bradygaster/archive/2003/11/26/39952.aspx
My Related posts:
Sample VS2008 project can be found on my SkyDrive: