Search code examples
c#.netiis-7httpmodulelifecycle

Earliest access to the .net lifecycle


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.


Solution

  • 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.