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"); } }