Session object is not available in HttpAplication's BeginAcquireState event in HTTP Module. RRS feed

  • Question

  • User-1019535860 posted


    I wrote a custom Session State HTTP module to handle many business policies to Session requests.

    I want to fire Session_Start Event in Application's Global.asax. according to the Microsoft's .NET 4.5 framework SessionStateModule Source Code i invoke Session Start event handler (hooked by asp.net) in Asynchronous httpApplication's  app_BeginAcquireState event in my Custom Session Module as follows:

    public void Init(HttpApplication app)
        // Handling OnAcquireRequestState Asynchronously
            new BeginEventHandler(this.app_BeginAcquireState),
            new EndEventHandler(this.app_EndAcquireState));
    private IAsyncResult app_BeginAcquireState(object source, EventArgs e, AsyncCallback cb, object extraData)
        if (_rqIsNewSession)
            // Firing Sessoin_Start Event
            _sessionStartEventHandler(this, EventArgs.Empty);

    but the problem is that the Session Object in Session_Start is not constructed and available yet and does not exist! and no key/value can be added to the Session at this time and i it throws Exception.

    i found this useful post as a solution to mark HttpContext's Handler as IRequiresSessionState and IReadOnlySessionState in Application_PostMapRequestHandler. i handled Start Invoking in AcquireState that is before PostAcquireRequestState also i added async event for BeginAcquireState to handle Concurrency on Session Request. so when i mark the HttpContext's Handler to IRequireSessionState in BeginAcquireState still Session is not available in MyModule_Start (Session_Start) event handler!

    Where should i handle this solution to have Session object to invoke Session Start event? or in which event should i handle my BeginAcquireState work that initializes new Sessions in SessionStoreProvider.

    Additional Info:

    notice that i have removed default session module and added MyCustomSessionStateModule:

        <remove name="Session"/>
        <add name="MyCustomSessionStateModule" type="CustomSessionStateServer.MyCustomSessionStateModule" />

    Monday, October 20, 2014 4:39 AM