Thursday, May 24, 2012

State Management Techniques in ASP.Net-Part2

In previous post State Management Techniques in ASp.Net, we discussed about State Management and Client-Side State Management Techniques. Today we will discuss about Server-Side State Management Techniques.

Server-Side State Management Techniques

In ASP.Net , we have a various ways to maintain the state information on server side,  rather than maintaining it on client side. Application, Session and Database are the different mechanism for sever side state management.


Using Server-Side state management, must care about the conservation of server resources.For a high traffic site which have large number of concurrent users, usages of session object for state management can create load on server, which can decrease the performance.

Application State

ASP.Net offers to save value on application state.We used the application object to store the data which is visible across the entire application and shared between multiple user sessions. Application object is an instance of HttpApplicationState class for each web application.

Application state is store in key/value dictionary that is create during each request to a specific URL. You can add your application-specific information to this structure to store it between page requests.Example:-

//save value to application state
Application.Lock();
Application["Message"]  "mydata";
Application.UnLock();

Session State

ASP.Net allows us to store data on session objects.Session object is used to store state information per client basis.Session object is an instance of HttpSessionState class for each active web application session.

Session state is similar to application state,except this that session state is scoped to the current browser session.If different users are using the application then each user session will have a different session state.
Session state can be configured using <sessionState> section in application's web config file.

//save value to session
Session["Message"]  "mydata";
//read value from session
string str=Session["Message"].ToString();

You can use session state to accomplish the following tasks: 
i. Uniquely identify browser or client-device requests and map them to individual session instances on the server. This allows you to track which pages a user saw on your site during a specific visit. 

ii. Store session-specific data on the server for use across multiple browser or client-device requests during the same session. This is perfect for storing shopping cart information. 

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

ASP.Net allows three types of session storage:-

(a.) InProc- This is the default session storage. Session data stores on server memory. InProc mode offers high performance as it reads from same processes memory This mode is good for simple applications.

(b.) StateServer- StateServer stores session state in a service called ASP.Net State Service.This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm. It runs as a separate windows service and keeps the session data out of ASP.NET process memory area. To access session, ASP.NET process has to communicate with this external process.

(c.) SQL Server- In this mode session state stores in SQL Server Database.It allows to keep session data in SQLServer. This is helpful when your web application is hosted in a webfarm.

Profile Properties

ASP.Net provides a feature called profile properties, which allows you to store user-specific data. This feature is similar to session state, except that the profile data is not lost when a user's session expires. The profile-properties feature uses an ASP.Net profile, which is stored in a persistent format and associated with an individual user. The ASP.Net profile allows you to easily manage user information without requiring you to create and maintain your own database.


No comments:

Post a Comment

^ Scroll to Top