Basic HttpModule Sample (Plus Bonus Case Study - How HttpModule Saved Mission Critical Project's Life)

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.

"no need to alter the application itself - just implement IHttpModule interface and tweak web.config"

Step 1 Create Class Library. Fire up Visual Studio. Click "New Project" dialog, click "Visual Studio Solutions" choose "Blank Solution" and create empty solution. Name it HttpModuleSample. Right click on the solution in "Solution Explorer" and then click "Add", "New Project...". Choose "Class Library" template and name it MyHttpModuleLib. Delete default Class1.cs. Add reference to System.Web assembly.

Step 2 Implement IHttpModule Interface. Add new class to the project and call it MyHttpModule. Add using System.Web declaration. Implement the IhttpModule interface just add :IHtpModule and let Visual Studio implement it explicitly. Register for any of ASP.NET pipeline events - BeginRequest for example - in Init(...) method. Set break point in the event handler.

image

Step 3 Configure ASP.NET application to use HttpModule. Right click on the solution in solution explorer and add new "ASP.NET Web Application" found under Web node in "Add New Project" dialog. Name it HttpModuleTestWeb. Open the application Web.Config file and add HttpModules node in case it is not already there: image Configure MyHttpModuleLib project output path to HttpModuleTestWeb's bin folder. To do so right click on MyHttpModuleLib and choose properties, go to Build section and then to "Output path" in out put section: image Build the solution. Ctrl+Shift+B.

Step 4 Test the application.   Set HttpModuleTestWeb as start up project and hit F5. You should hit the break point set inside the HttpModule even before the Web Application's page was accessed: image 

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.

Case study

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.

Caveats

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 - https://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 - https://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 - https://weblogs.asp.net/bradygaster/archive/2003/11/26/39952.aspx

My Related posts:

Sample VS2008 project can be found on my SkyDrive: