views:

174

answers:

2

After looking at the .net on IIS7 application lifecycle:

http://msdn.microsoft.com/en-us/library/ms178473.aspx

For maximum performance, I want to find a way to get my code started as soon as the HttpContext object is created but before HttpApplication is. (it's easy to run code after the HttpApplication class is loaded but before any of it's event are triggered by using the contructor of an HTTP Module like this:

    public class AuthModule : IHttpModule 
    {     
        public AuthModule()
        {
            HttpContext.Current.Response.Write("hello world");
            HttpContext.Current.Response.End();
        }

        #region IHttpModule Members

        public void Dispose()
        {  }

        public void Init(HttpApplication context)
        {   }

        #endregion
    }

I know that i won't get access to the User object, but i won't need it.

A: 

Look at the life-cycle events on MSDN. You can consider using one of those events if you want something earlier than the normal page events.

ajma
His read it already - it's the first statement in his post, which you obviously didn't read very thoroughly!
Tor Haugen
+1  A: 

You cannot ever be sure your code starts before the HttpApplication instance is created, since these instances may be reused.

Also, running code at this stage is beyond the scope of the pipeline. It should make you ask yourself whether it's really a sensible thing to do.

And what's this about performance? You really think the time to create an instance of HttpApplication is going to register in your performance?

Take a step back and reconsider.

Tor Haugen
Digging through this has brought me to a much better understanding of modules, and handlers (which is even how .net handles all of it's pages) and i now see that it tries to reuse as much as possible over several contexts so stuff before HttpApplication would get "cached".
rizzle
I used the kernel to time the ticks passed of each even in HttpApplication and it takes up .2 seconds, which is a lot, but it only takes that long the first time after complie and was then caching the results so i didn't notice that it takes much less time the subsequent requests. Thanks!!
rizzle