ASP.NET segment is NULL despite IRequireSessionState / IReadOnlySessionState handler?

While studying another problem , I came across a peculiar situation.

Within the Global.asax method, I have the following code:

protected void Application_AcquireRequestState(object sender, EventArgs e)
{
    if (!(Context.Handler is IRequiresSessionState || Context.Handler is IReadOnlySessionState)) // No session - no validation.
        return;
    DoSomething();
}

      

The method DoSomething()

call calls a NullReferenceException

, and it's simple enough that the only place it could do was if HttpContext.Current.Session

it was null.

How can it be?

+2


source to share


2 answers


At this time, it HttpContext.Current.Session

may be zero. The event fires the moment the sessiondoes not guarantee that it currently has ... One of the reasons you might want to bind this event is because you provide your own module to implement your own Session object. This is where such a custom module would establish a session.



If you want to use a Session object, you must use an event PostAquireRequestState

.

+1


source


In this method, the Session object has not yet been created, so it is not available. Try to get the job done in any method that runs after this method. Maybe Application_PreRequestHandlerExecute or have a look at this page to see the order of method execution in the global.asax file https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-5771721. html

The remaining events are for application requests, and they are fired in the following order:



* Application_BeginRequest
* Application_AuthenticateRequest
* Application_AuthorizeRequest
* Application_ResolveRequestCache
* Application_AcquireRequestState
* Application_PreRequestHandlerExecute
* Application_PreSendRequestHeaders
* Application_PreSendRequestContent
* <<code is executed>>
* Application_PostRequestHandlerExecute
* Application_ReleaseRequestState
* Application_UpdateRequestCache
* Application_EndRequest

      

0


source







All Articles