Asp.net Server Side State Management

Wednesday, June 23, 2010


No web application framework, no matter how advanced, can change that HTTP is a stateless protocol. So inherently we should also forget our users, but unfortunately we cannot.ASP.Net Framework provides us features by which we can maintain states of your users. We can Manage State on Application ans Session Level
  • Session State
  • Application State
Session State:
It stores user specific information on server side.
There are 5 modes which a user can define for Session States;
  1. Off

  1. InProc

  1. StateServer

  1. SQLServer
  1. Custom
Sessions can and can not be cookie based.
The default name for this Session based cookie is Asp.Ner_SessionId We

Session state is structured as a key/value dictionary for storing session-specific information that needs to be maintained between server round trips and between requests for pages.
You can use session state to accomplish the following tasks:

Uniquely identify browser or client-device requests and map them to an individual session instance on the server.

Store session-specific data on the server for use across multiple browser or client-device requests within the same session.

Raise appropriate session management events. In addition, you can write application code leveraging these events

Sessions (In Proc)
When the configuration is set to InProc, session data is stored in the HTTPRuntime’s internal cache in an implementation of ISessionItemCollection that implements ICollection.
Stores Session in ASP.Net in-memory cache.


Sessions (State Server)
Also known as out of process.
Sessions states are held in a process called aspnet_state.exe that runs as a Windows Service.
Objects to be stored in StateServer sessions requires to be Serializable.
By default State Service is not running.

timeout=“20”/>
>
Sessions (Sql Server)
Sql Server based state management is not effected by farming or gardening issues.
Enable Sql Server support by running aspnet regsql -? Command to access and enable Session State Option.
Aspnet_regsql –S machine\servername –d statemanagement –U sa –P new –ssadd –sstype c
Sessions Recommendations
  • InProc is the fastest but most unreliable
  • StateServer is a better option but requires Serialization. It is slower than InProc as objects physically leave the application.
  • SQLServer is the most reliable and robust but is the slowest but also helpful when it comes to application scalability.
Declaring a Session Variable



string firstName = "Usama";
string lastName = "Khan";
string city = "Karachi";
Session["FirstName"] = firstName;
Session["LastName"] = lastName;
Session["City"] = city;
Using Session Variable




TxtfirstName
.Text
=
Session["FirstName"];
Txt
lastName
.Text
=
Session["LastName"]
;
Txt
city.Text =
Session["City"]
;

Application State
Application state is a data repository available to all classes in an ASP.NET application. Application state is stored in memory on the server and is faster than storing and retrieving information in a database. Unlike session state, which is specific to a single user session, application state applies to all users and sessions. Therefore, application state is a useful place to store small amounts of often-used data that does not change from one user to another
Global does not mean global to the machine, but global to the application.
Look for global.asax file.

Application state is stored in an instance of the HttpApplicationState class. This class exposes a key-value dictionary of objects.

Declaring Application Variable

In Application_Start handler of your application's Global.asax

Application["Message"] = "Welcome to Evolution Technologies ";

Application["PageRequestCount"] = 0;

Application.Lock();

Application["PageRequestCount"] =

((int)Application["PageRequestCount"])+1;

Application.UnLock();


share this post
Share to Facebook Share to Twitter Share to Google+ Share to Stumble Upon Share to Evernote Share to Blogger Share to Email Share to Yahoo Messenger More...

0 comments: