Click here to Skip to main content
15,888,984 members
Articles / Web Development / ASP.NET
Tip/Trick

HTTP Module Pipeline and Session Creation in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.83/5 (3 votes)
6 Mar 2017CPOL1 min read 10.2K   1   3
This article explains the Session creation during HTTP Module pipeline

Introduction

Many of us are not able to understand the best place to create or access the session object earliest in the page execution. Here, I am trying to elaborate the details so this can be understood in the best way.

We have the below events in HTTP Module pipeline:

(Pre - Page Life Cycle)

  1. BeginRequest
  2. AuthenticateRequest
  3. AuthorizeRequest
  4. ResolveRequestCache
  5. AcquireRequestState
  6. PreRequestHandlerExecute

Post - Page life cycle

  1. PostRequestHandlerExecute
  2. ReleaseRequestState
  3. UpdateRequestCache
  4. EndRequest

The session is created during AcquireRequestState event.

Now, as we are aware, the first event in page life cycle is PreInit. If anyone tries to access the session at the top of the class itself, before PreInit method, it is not possible. Please see the example below:

C#
public class MyPage : System.Web.UI.Page {
  private MyClass _myObj = new MyClass();
  ...

Here, we are trying to create the object of MyClass at the top of MyPage class before PreInit method. This MyClass has constructor as below:

C#
public MyClass() 
{
  var mySession= HttpContext.Current.Session; // no problem here
  mySession["MyValue"] = 123;                 // throws null ref exception coz mySessionis null
}

This code will throw an exception because session handling is done at the wrong place.

The reason being:

In the request life cycle, this process of creation of object and execution of constructor is done during ResolveRequestCache, i.e., before AcquireRequestState event (since this is directly created in class and not any method). As expected, the session will still not be available in the constructor as AcquireRequestState is not yet executed.

PreInit method in page life cycle is called after AcquireRequestState and hence the session is available there.

Below is the sequence of execution of Page Life Cycle events:

  1. PreInit
  2. Init
  3. InitComplete
  4. PreLoad
  5. Load
  6. Load Complete
  7. PreRender
  8. PreRenderComplete
  9. SaveStateComplete

Conclusion

Earliest session operations can be done anytime after AcquireRequestState in Http Modules (if you are intercepting the request programmatically) or in/after PreInit method.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIs this same pipeline for mvc Pin
Ankit Rana6-Mar-17 0:20
Ankit Rana6-Mar-17 0:20 
AnswerRe: Is this same pipeline for mvc Pin
Member 1304024214-Mar-17 0:13
Member 1304024214-Mar-17 0:13 
QuestionRe: Is this same pipeline for mvc Pin
Ankit Rana16-Mar-17 20:37
Ankit Rana16-Mar-17 20:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.