Sunday, December 6, 2009

ASP.NET Session State

ASP.NET Session State

ASP.NET session state enables you to store and retrieve values for a user as the user navigates to different ASP.NET pages that make up a Web application. HTTP is a stateless protocol, meaning that your Web server treats each HTTP request for a page as an independent request; by default, the server retains no knowledge of variable values used during previous requests. As a result, building Web applications that need to maintain some cross-request state information (applications that implement shopping carts, data scrolling, and so on) can be a challenge. ASP.NET session state identifies requests received from the same browser during a limited period of time as a session, and provides the ability to persist variable values for the duration of that session.
ASP.NET session state is enabled by default for all ASP.NET applications. ASP.NET session-state variables are easily set and retrieved using the Session property, which stores session variable values as a collection indexed by name. For example, you can create create the session variables FirstName and LastName to represent the first name and last name of a user, and sets them to values retrieved from TextBox controls.
Basic use of Session in ASP.NET (C#):
STORE: Session["mydataset")=ds;
RETRIEVE: DataSet ds = (DataSet)Session["mydataset"];
Storage location
InProc - session kept as live objects in web server (aspnet_wp.exe).
StateServer - session serialized and stored in memory in a separate process (aspnet_state.exe). State Server can run on another machine
SQLServer - session serialized and stored in SQL server
Performance
InProc
- Fastest, but the more session data, the more memory is consumed on the web server, and that can affect performance.
StateServer - When storing data of basic types (e.g. string, integer, etc), in one test environment it's 15% slower than InProc. However, the cost of serialization/deserialization can affect performance if you're storing lots of objects. You have to do performance testing for your own scenario.
SQLServer - When storing data of basic types (e.g. string, integer, etc), in one test environment it's 25% slower than InProc. Same warning about serialization as in StateServer.

No comments:

Post a Comment