johnllao

June 5, 2009

Session_Start and Session_End event from custom HttpApplication

Filed under: Uncategorized — johnllao @ 7:23 pm

Example that shows how to assign Session_Start and Session_End events from a custom HttpApplication object.

By default HttpApplication only contains events related to a particular request such as BeginRequest, so in order to initiate the Session_Start and Session_End event one has to get reference to the SessionStateModule. SessionStateModule is one of the HttpModule that was attached to a web application, this module handles the session management. SessionHttpModule is also a sealed class so we cannot inherit from it, so the only way to intercept the Session_Start and Session_End events is to get reference of this module.

Please see the code below.

    public class KyuApplication : System.Web.HttpApplication
    {
        public override void Init()
        {
            SessionStateModule session = Modules["Session"] as SessionStateModule;
            if (session != null)
            {
                session.Start += new EventHandler(Session_Start);
                session.End += new EventHandler(Session_End);
            }
        }

        private void Session_Start(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Session_Start");
        }

        private void Session_End(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Session_End");
        }
    }

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.

Blog at WordPress.com.