Santry Technology Solutions, Content Management, DotNetNuke, SharePoint Consulting
Register | Login
Wednesday, January 07, 2009

Sections
  
About Us
  
Partners
Downloads
  
 WWWCoder.com Resource Directory

Using HTTPModules in a Multiple Application Environment
9/19/2003 6:17:32 PM

In this article Tom Lucas introduces you to configuring the Web.config for using HTTPModules. This article will briefly explain the basic concepts of ASP.Net configurations and then show one possible solution to using HTTPModules in a multiple application environment

Many developers using ASP.Net have found the use of HTTPModules a great benefit to this emerging platform. HTTPModules allow the developer to intercept certain requests and perform some process on the request. One example would be to write an HTTPModule that captures all requests for a new file extension the developer wants to handle. At the same time, many people have had confusion when using multiple applications under one website where HTTPModules are used. This article will briefly explain the basic concepts of ASP.Net configurations and then show one possible solution to using HTTPModules in a multiple application environment.

Let us remember that the configuration files in ASP.Net applications are hierarchical. Think about a pyramid. The top point of the pyramid is our starting point. Now we pour a barrel of water over the top of the pyramid. What happens? The water flows down from the top of the pyramid down to the bottom, expanding to cover the face as it descends.

ASP.Net application configuration work in a similar fashion. That means that whatever settings are configured at the top of the pyramid get applied to everything below unless explicitly changed. So, what is the top of the pyramid? Well it is an often forgotten configuration file called the machine.config file. This file is located in the %windir%\Microsoft.NET\Framework\Version\CONFIG directory. This is akin to the master control file. The settings here affect all ASP.Net applications running on the server. That's pretty powerful stuff when you stop to think about it.

Next, are the building blocks. The block of our pyramid are the applications running on the server. Each application inherits its initial settings from the machine.config. Now how do we set configuration for the current application you ask? That is the web.config file. The web.config file can exist for every application. This file has many settings that are available. Each setting is part of various configuration sections in these configuration files.

One section in the web.config concerning this is the httpModules section. This section is where the HTTPModules are registered for the application.


<configuration>
<system.web>
<httpmodules>
<add type="System.Web.State.CacheModule" name="OutputCache" />
<add type="System.Web.State.SessionStateModule" name="Session" />
</httpmodules>
</system.web>
</configuration>

So it's easy enough to add the HTTPModule. Now why when I run another application does it try to load the HTTPModule even though the application web.config file does not add it? Let's take the following example application / virtual directory structure.


MainApp1/
SubApp1/
SubApp2/
SubApp3/


So MainApp1 is our root level application. Easy enough. This is like installing your application in the root of a website. Now let's say that we use an HTTPModule in the MainApp1 application and configure it in our web.config file as follows.


<configuration>
<system.web>
<httpmodules>
<add name="HTTPModule" type="DotNetNuke.HTTPModule, DotNetNuke" />
</httpmodules>
</system.web>
</configuration>

Ok, so now our application has registered and can use this HTTPModule. Now on to SubApp1. This is a complete different application setup as a new application in the IIS administration console. It has it's own web.config file that does not even contain a single section. Unfortunately, when we run the application we get an error from ASP.Net stating that the HTTPHandle DotNetNuke could not be loaded as the .dll file could not be found. What?!?!?

Let me explain. What has happened is that IIS has handed you request for SubApp1 off to the Aspnet_isapi.dll. This is the .dll that handles all ASP.Net requests in IIS. Now remember our hierarchy; ASP.Net loads the configuration first from the Machine.config file. Then the application directory structure is traversed. The MainApp1 web.config file is found, read and used. Hmmmm....that loaded the HTTPModule declaration. Finally the web.config file for our SubApp1 application is read. As you may be able to see by now, the HTTPModule was loaded from the MainApp1 web.config and is still in place when SubApp1 runs.

So how do we fix this? It's easy. Remember that settings are inherited unless explicitly changed. Now it is time for us to explicitly change them for our application. Not only can we add httpModules in the web.config file, but we can also remove individual ones or even clear them all. That is what we are going to do, by adding the following to our web.config file for SubApp1 we are clearing all HTTPModules explicitly.


<configuration>
<system.web>
<httpmodules>
<clear />
</httpmodules>
</system.web>
</configuration>

That is all there is to it. Our SubApp1 runs fine now. One more quick note. Look at SubApp2. This application is also below MainApp1. If we do not change the web.config file for SubApp2, it will attempt to use the HTTPModule declared in MainApp1. This may indeed be the behavior you want, however if it is not, you now have the power of knowing how to change it.


By: Tom Lucas - Principle of LucasLabs.Net and PortalPro.Net. Tom has over 10 years of experience in web appliction design, achitecture and development. In addition, Tom has won several awards for his law enforcement and municipal government applications and has been an invited speaker at a number of local and international law enforcement conferences. You can view his personal website at http://www.lucaslabs.net/.

Related Articles
   Related Document Making Your App Spider Friendly
   Related Document Using HTTPModules in a Multiple Application Environment


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 1/26/2007 2:38:15 PM by Anonymous
Comments: it works, but you have to remember that it's httpModules, not httpmodules.  aside from this case-sensitivity issue, the article is spot-on, so shuddup haters!
Left on 4/24/2006 6:09:54 PM by Anonymous
Comments: This doesn't work
Left on 4/6/2006 3:45:00 PM by Anonymous
Comments: to remove each type



No ratings available.
Left on 2/1/2006 3:11:25 AM by Anonymous
Comments: very clear explanation.
No ratings available.
Left on 7/28/2005 11:35:31 PM by Anonymous
Comments: yeah - this is a microsoft boo boo IMHO :(
Seems to me that it does exactly what the MSDN documentation says - but it executes each directive in order.
ie - in the sub-app's initialisation the parent application's "add" is executed *then* it does the "clear" or "remove" specified in the child's web.config.
This means the sub-app still needs the HttpModule's dll in it's "bin" directory so that it can be added and *then* immediately removed :/
sweet :(
No ratings available.
Left on 6/16/2005 11:08:41 AM by Anonymous
Comments: Would be great if it was working.
Left on 4/20/2005 6:28:13 PM by Anonymous
Comments: This did not work for me
No ratings available.
Left on 3/4/2005 10:35:52 PM by Anonymous
Comments: Sub App should have Bin Directory and should contain HttpModule Dll, App Dll. Thats means one ways its a new Application, so there is no concept of Sub Applicaton. HttpModule element is ignore in Sub Directory.
No ratings available.
Left on 3/4/2005 10:31:19 PM by Anonymous
Comments: Only the way this will work when you mark the sub Application will have Bin folder with HttpModules under it. Make sure the you have mark the sub Application as Virtual.
No ratings available.
Left on 3/1/2005 6:17:30 PM by Anonymous
Comments: "That is all there is to it. Our SubApp1 runs fine now."  Except our sub app doesn't run fine now.  Please explain.
No ratings available.
Left on 2/8/2005 5:24:56 AM by Anonymous
Comments: when I tried to remove the http modules using clear . It didn't work.Is there somthing more to do?
No ratings available.
Left on 1/26/2005 11:56:56 AM by Anonymous
Comments: Hi,

I've exactly configured my web so, but it doesn't run...
No ratings available.
Left on 12/23/2004 12:58:37 PM by Anonymous
Comments: htmlModules, M has to be capitalized.
Left on 10/27/2004 7:05:25 PM by Anonymous
Comments: What about the flip side of this scenario?  If I want to add an HttpModule in machine.config that defines a behaviour for every request to the server, how do I then prevent applications from 'removing' or 'clearing' this module?
Left on 10/23/2004 2:04:01 PM by Anonymous
Comments: and why doesnt this work?
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/gngrfhttpmodulessection.asp
No ratings available.
Left on 10/8/2004 12:30:23 PM by Anonymous
Comments: You mentioned how to remove individual modules, but then only showed how to clear ALL of them...
Left on 10/1/2004 3:53:03 PM by Anonymous
Comments: Would have been worth a 2 or a 3 if you showed some examples.
Left on 2/17/2004 4:27:31 PM by Anonymous
Comments: And if this still doesn't work?
  

 Latest Articles
  

 Latest News
  

 

Spotlight
Syndication

 


 


Digg This
 


DotNetNuke Platinum Benefactor

  
 

 Terms Of Use | Privacy Statement
 Copyright 2008 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970