Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are there two incompatible session state types in ASP.NET?

I see two means of working with session data in ASP.NET MVC:

  • System.Web.SessionState.HttpSessionState, available on HttpApplication
  • System.Web.HttpSessionStateBase, available on Controller

Data stored in one seems to be available in the other.

Unfortunately the only common ancestor of these two types is System.Object, meaning that I can't create reusable utility code for the abstraction of either.

Why is the API this way? Is there an important difference between the two that I am missing?

like image 621
Drew Noakes Avatar asked Mar 27 '11 06:03

Drew Noakes


People also ask

How many types of session state is available in ASP NET core?

Sessions are of two types, namely In-Proc or In-memory and Out-Proc or Distributed session.

What is the difference between session and Httpcontext current session?

There is no difference. The getter for Page. Session returns the context session.

What is session state used for?

Session state is an ASP.NET Core scenario for storage of user data while the user browses a web app. Session state uses a store maintained by the app to persist data across requests from a client. The session data is backed by a cache and considered ephemeral data.


1 Answers

In ASP.NET MVC abstractions over the classic HttpContext objects Request, Response, Session were introduced. They represent abstract classes and are exposed all over the MVC framework to hide the underlying context and simplify the unit testing because abstract classes can be mocked.

For example for the session object you have HttpSessionStateBase and its implementation HttpSessionStateWrapper.

Here's an example of how to convert between the classic ASP.NET session and the abstraction:

HttpSessionStateBase session = new HttpSessionStateWrapper(HttpContext.Current.Session); 

So the System.Web.SessionState.HttpSessionState which you are referring to is the underlying session object which existed ever since classic ASP.NET 1.0. In MVC this object is wrapped into a HttpSessionStateWrapper. But since ASP.NET MVC is an ASP.NET application you still get the Global.asax in which you have the bare session.

like image 134
Darin Dimitrov Avatar answered Sep 23 '22 20:09

Darin Dimitrov