Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top