The SessionStateModule.End event is raised at the end of a request when the HttpSessionState.Abandon method has been called or when the session has expired. A session expires when the number of minutes specified by the HttpSessionState.Timeout property passes without a request being made for the session.
The Session_OnEnd event is used to perform any cleanup work for a session such as disposing of resources used by the session.
You can specify a handler for the SessionStateModule.End event by adding a public subroutine named Session_OnEnd to the Global.asax file.
The Session_OnEnd event is only supported when the session-state HttpSessionState.Mode property value is SessionStateMode.InProc, which is the default. If the session-state HttpSessionState.Mode is set to SessionStateMode.StateServer or SessionStateMode.SQLServer, then the Session_OnEnd event in the Global.asax file is ignored. If the session state HttpSessionState.Mode property value is SessionStateMode.Custom, then support for the Session_OnEnd event is determined by the custom session-state store provider.
Though the SessionStateModule.End event is public, you can only handle it by adding an event handler in the Global.asax file. This restriction is implemented because System.Web.HttpApplication instances are reused for performance. When a session expires, only the Session_OnEnd event specified in the Global.asax file is executed, to prevent code from calling an SessionStateModule.End event handler associated with an System.Web.HttpApplication instance that is currently in use.
For more information about the Global.asax file, see Global.asax Syntax.