This blog comes from a colleague and a discussion of a mistake in the company's project. The error is very simple, just in the constructor in a page:
Session ["AAA"] = "BBB";
When it will throw an exception, saying the session in the system is not enable, so it cannot be used (of course, the actual situation is definitely being enabled). The problem is also found soon, and the session can be used in the constructor, and this is OK in Page_init.
The root cause of this error should be that when the constructor is called, the corresponding initialization session code in the ASP.NET engine is not executed in the ASP.NET engine has not been executed, so it has caused the SESSION information that cannot be accessed at that time. .
SessionStateModule In its init method, the AcquireRequestState event in HTTPApplication is registered on the corresponding method to complete the corresponding method by using the AddonAcquireRequestStateAsync () method to perform asynchronous event processing. Assign the current httpContext's session property, while the page object has been created, that is, the execution of the constructor of the page is called before the AcquireRequestState event is called, so in the page constructor Access Session will certainly trigger the corresponding exception.
The exact execution order of HTTPApplication is:
BeginRequest -> AuthenticateRequest -> AuthorizeRequest -> ResolveRequestCache -> build page (here page constructor is called) -> AcquireRequestState (here only initialize the current context Session) -> PreRequestHandlerExecute -> to enter the page execution lifecycle (start Page_Init) - > PostRequestHandleRexecute -> ReleaseRequestState -> UpdateRequestCache -> endRequest