Shaul Behr
Shaul Behr

Reputation: 38033

When is a Session created?

On my login web page (i.e. the first page the user hits) I have code of this form:

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

MyClass has the constructor:

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

As I've commented, the call to sess["MyValue"] throws a null reference exception, because the HttpContext hasn't yet created the session object.

So when is it safe to start accessing the Session object? I don't want to give up on the inline declaration/initialization of _obj; that's very convenient for me! What alternatives do I have?

Upvotes: 1

Views: 7088

Answers (4)

Akshay Bhagwat
Akshay Bhagwat

Reputation: 41

The session is created in aquireRequestState event of Http Module pipeline. I have tried to explain this whole thing below in detail.

We have 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
7. PostRequestHandlerExecute
8. ReleaseRequestState
9. UpdateRequestCache
10. EndRequest

Now, as we are aware, the first event in page life cycle is PreInit. You are trying to create the object _obj before PreInit event. 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.

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.

Upvotes: 4

Ryan Alford
Ryan Alford

Reputation: 7594

If the user is on the first page of the site, then the Session has no objects in it. You should definitely check the value before assigning to it.

if (Session["MyValue"] == null)
   Session.Add("MyValue", "");

Session["MyValue"] = "123";

You can also add the Global.asax file to the project. That is where you can handle the Session_Start event.

Upvotes: 2

ram
ram

Reputation: 11626

You can look at the http pipeline. Rich Starhl has a nice article. . Your session objects are created/retrieved during aquireRequestState event. If you have HTTP Modules which intercept the request before this event, your session might not have been initialized

Upvotes: 1

kͩeͣmͮpͥ ͩ
kͩeͣmͮpͥ ͩ

Reputation: 7846

You should be able to access the Session in or after the Page's OnInit event, or PreInit if you're that bothered. Before that, and you're dicing with death.

Upvotes: 14

Related Questions