79

What is the difference between Session and HttpContext.Current.Session object?

Andrei
  • 42,814
  • 35
  • 154
  • 218
Bhaskar
  • 4,189
  • 4
  • 26
  • 20
  • 4
    It is worth clarifying that when you say 'session', you are referring to System.Web.UI.Page.Session. The Session object is available inside the context of the ASP.NET page. – Llyle Oct 11 '10 at 23:45

4 Answers4

117

A little late here, but here's something I just discovered.

@Phillipe Leybaert and @CSharpAtl are both incorrect. HttpApplication's Session property exhibits different behaviour than does that of the property HttpContext.Current.Session. They will both return a reference to the same HttpSessionState instance if one is available. They differ in what they do when there is no instance of HttpSessionState available for the current request.

Not all HttpHandlers provide session state. To do so, the HttpHandler must implement [one or both?] the marker interfaces IRequiresSessionState or IReadOnlySessionState.

HttpContext.Current.Session simply returns null if there is no session available.

The HttpApplication's implementation of the Session property throws an HttpException with the message Session state is not available in this context. rather than returning a null reference.

Some examples of HttpHandler that do not implement session are the default handlers for normally static resources, such as image and CSS files. Any reference to the HttpApplication's Session property in such cases (as in global.asax event handlers) will result an HttpException being thrown.

Needless to say, the unexpected HttpException provides a WTF?! moment if you're not expecting it.

The Session property of the HttpApplication class is implemented thus (from Reflector):

[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public HttpSessionState Session
{
  get
  {
    HttpSessionState session = null;

    if (this._session != null)
    {
        session = this._session;
    }
    else if (this._context != null)
    {
        session = this._context.Session;
    }

    if (session == null)
    {
        throw new HttpException(SR.GetString("Session_not_available"));
    }

    return session;
  }
}
dey.shin
  • 990
  • 10
  • 21
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
  • 9
    Thanks for putting the effort in to fill out a better answer. – nicodemus13 May 26 '11 at 10:19
  • 12
    No problem. I had just had a rather annoying WTF? moment that took some time to sort out. I figured I'd document it so somebody else down the line wouldn't have to spend the time figuring out what was going on. – Nicholas Carey May 26 '11 at 16:52
8

There is no difference.

The getter for Page.Session returns the context session.

Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
2

Nothing. Session just points to the HttpContext.Current.Session.

dey.shin
  • 990
  • 10
  • 21
CSharpAtl
  • 7,374
  • 8
  • 39
  • 53
1

Internally, Page.Session points to It's HttpContext.Current.Session only, but there are still two differences depending on from where it's called.

Page.Session can be accessed only from classes inherited from System.Web.UI.Page and it will throw HttpException when accessed from WebMethod.
Where as HttpContext.Current.Session can be accessed from anywhere as long as you're running in the context of a web application.


Other important difference where you can access Page.Session but cannot access HttpContext.Current.Session :

If there is a method named GetData in your page(inherited from System.Web.UI.Page) which is executed concurrently in different threads from some other page method, GetData method can access the Page.Seession, but you cannot access HttpContext.Current.Session.

It's because GetData has been called from different thread so HttpContext.Current is null and HttpContext.Current.Session will throw null reference exception, but Page.Session will still be attached with page object so page method GetData can access the Page.Session.

Jay Shah
  • 3,553
  • 1
  • 27
  • 26