Thursday, November 26, 2009

ASP.Net Application state, difference between application, viewstate & session state

What is ASP.NET 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 all 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.
What is Postback?
When an action occurs (like button click), the page containing all the controls within the tag performs an HTTP POST, while having itself as the target URL. This is called Postback.
ASP.NET View State
ASP.NET Web pages provide view state, which is a way for you to store information directly in the page that you want to persist between postbacks.
ASP.NET Cookies
A cookie is a small bit of text that accompanies requests and pages as they go between the Web server and browser. The cookie contains information the Web application can read whenever the user visits the site. Creating CookiesResponse.Cookies["userName"].Value = "patrick";Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1); HttpCookie aCookie = new HttpCookie("lastVisit");aCookie.Value = DateTime.Now.ToString();aCookie.Expires = DateTime.Now.AddDays(1);Response.Cookies.Add(aCookie);
Getting Cookiesif(Request.Cookies["userInfo"] != null){ System.Collections.Specialized.NameValueCollection UserInfoCookieCollection; UserInfoCookieCollection = Request.Cookies["userInfo"].Values; Label1.Text = Server.HtmlEncode(UserInfoCookieCollection["userName"]); Label2.Text = Server.HtmlEncode(UserInfoCookieCollection["lastVisit"]);}

Differences between Session and ViewStates
"Session" is data stored per user session. "Viewstate" is an ASP.NET thing. It's a way for the application to maintain state about a particular form and its child controls. Your ASP.NET application might consist of several pages/forms. There might be a piece of data that you want to access across all pages. You could place that in a Session variable. However, you rarely need to worry about ViewState, ASP.NET manages that itself.

Viewstate variables are stored in the HTML of your page. They are encoded so they are not easily readable, but they are not encrypted. This is generally the best scope to use when you have a variable that is valid for a particular page and you want to store the value between postbacks. Once theuser navigates to another page the value is lost.Session variables are stored on the server (in RAM by default) and are persisted between all pages.

Different between application and session state
The difference is in the length of their existance.An application variable exists from when the application is first 'run' (The first time a page in the app is used) and exists until the application is stopped on the server, either through IIS or the actual machine being turned off or rebooted.A session variable is created for the instance of the browser accessing the site and will only be available until that browser is closed or the session times out.

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.