views:

191

answers:

1

I have a class that implements IHttpModule in a separate assembly from a website. The module implementation intercepts requests and rewrites urls for the website.

The mappings are stored in a class with the requested url and the destination url.

Is the second example, MTSingleton, from http://devhood.com/Tutorials/tutorial_details.aspx?tutorial_id=486 suitable for creating the mapping list? Is there a better approach from within the module implementation?

Edit: My bad, this is for IIS 6.0 and .NET 3.5 SP1

+1  A: 

Sounds like you're looking to create the mappings object once in your app cycle. It sounds like you're trying to prevent this from being created over and over per request. (Please clarify if I'm wrong.)

Look at the methods on IHttpModule. Assuming you're working with IIS 7.0, the ASP.Net lifecycle will show that the Init() method is fired once. Meaning, it's fired once per application lifecycle. So, fire up the web server, first request will kick Init() into gear, then subsequent requests don't need to fire it until the web server application cycle is refreshed.

You should be able to safely move your mappings creation code into the Init() method, which should provide you with the safeguards you're seeking with a multi-threaded singleton type of initialization. You should still have multi-threaded safeguards around your mapping object, but the IHttpModule's Init() method should give you the fire-once-and-done effect you're seeking.

jro
Yes, the first paragraph is what I am trying to accomplish. I am using IIS 6.0, does Init still have the same behavior?
blu
This seems to behave the same in IIS 6.0, Init is called once.
blu
I believe IIS6 exhibits the same behavior, yes.
jro
This was a much simpler solution than I was planning, thanks.
blu
jro - that isn't entirely correct. the Init() method can actually get fired multiple times within a single AppDomain. The ASP.NET worker process instantiates multiple HttpApplication objects and then pools them (for performance), and a copy of each registered IHttpModule is instantiated for each HttpApplication (notice that HttpApplication object is passed into the Init). Quote from the URL you sent: "...first time ASP.NET page ... is requested...a new instance of the HttpApplication class is created. ...to maximize performance, HttpApplication instances might be reused for multiple requests."
Sunday Ironfoot
Ironfoot - that is correct, it can fire aggregately multiple times. However, in the case of an HTTP module, an Init() method would need to fire for each instance. The original goal was to deal with a singleton object, which still applies on a per-appcycle basis. In spite of firing multiple times, each will function as singleton inside their respective process. But good on clarifying that there can be multiple firings per app domain.
jro