Asp. NET state management

Source: Internet
Author: User
Tags session id server memory
The asp.net asp.net provides four types of states: Application,session,cookie,view.

The application state provides a global state for the application. This state is available to all customers. From a design standpoint, we usually use application to store some standard data. At the same time, we should use it to avoid performance degradation, stored data as much as possible to provide customers with read-only functionality.

We can use the Application property of the HttpApplication class to access the application state, which returns an instance of the HttpApplicationState class. This class is a collection of objects that can store any type of data and store it as a key/value pair. Once the data is stored in the state, it is not deleted unless the application restarts or is terminated or reclaimed.

We can store the data in the Global.asax Application_Start function:
void Application_Start (Object src, EventArgs e)
{
int exp = 0;
Population of DataSet from Ado.net query not shown
Cache DataSet Reference
Application["experiment"] = exp;
}

Now you can use it on any page:

private void Page_Load (Object src, EventArgs e)
{
int expr = Int32.Parse ((application["Experiment"));
}

Because the application state is shared for all customers, if the customer simply reads the data, there is no problem, and once the write is done, there is no guarantee of thread security and synchronization contention issues. We can use the Httpapplicationstatelock class, which derives from the Readwriteobjectlock class, which provides two properties for read/write locks. Under ASP.net, the Acquirewrite () and Acquireread () methods are implicitly invoked to ensure that the above problems are avoided. Of course, we can also display the use of lock () and Unlock ():

private void Page_Load (object sender, System.EventArgs e)
{
Application.Lock ();
int expr = Int32.Parse ((application["Experiment"));
if (expr>=something)
{
Do something
}
Else
{
Do something else
}
Application.UnLock ();
Some other thing goes
}


The Session,cookie,view state is used to hold the client information. What difference does it have between them?

The session state is created when the customer logs on, and it holds the customer-specific information and is identified with the session ID. When a new client accesses the application, the husband becomes a new session ID (or session Key) and creates a contact for the next request from the same customer. You can store any type of data in the session state, which, as your application, is maintained by the same process and AppDomain (app domain). The session state is characterized by creating states for each particular customer to maintain customer information that is stored in the server-side default sessions state configuration.

Session ("Value") = expr; Storing the data into session object
SomeFunction ()
{
int expr = Int32.Parse (Session ("Value")),//accessing from it
if (expr>=something)
{
Do something
}
Else
{
Do something else
}
Some other thing goes
}

Since session state is established for a particular customer, it is used to identify the customer's request. ASP.net provides a cryptographic mechanism and an encoding algorithm to generate its own session Key. This is necessary because knowing your session Key will have access to the specified page.

method to generate session key in asp.net:

byte[] SessionKey = new BYTE[15];

Generates a random number
RNGCryptoServiceProvider Rngkey = new RNGCryptoServiceProvider ();
Rngkey. GetBytes (SessionKey);
String clientsessionkey = Sessionid.encode (SessionKey);

But the session is related to the client's cookie, and the session fails when the client turns off the cookie. But in ASP.net,
You can modify the settings in Web.config to make the session pass out of the cookie. The method is:
<configuration>
<system.web>
<sessionstate cookieless= "true"/>
</system.web>
</configuration>


It is no stranger to cookies, and each cookie stores multiple name/value pairs, which we can access through the HttpCookie class's collection of values, or indirectly through the indexer provided by the class. The use of cookies under asp.net:
protected void Page_Load (Object sender, EventArgs E)
{
int expr = 0;
if (request.cookies["Expr"] = = null)
{
"Expr" Cookie not set, set and this response
HttpCookie cokexpr = new HttpCookie ("Expr");
Cokexpr.value = Exprtextbox.text;
RESPONSE.COOKIES.ADD (cokexpr);
Expr = Convert.ToInt32 (exprtextbox.text);
}
Else
{
Use existing cookie value ...
Expr = Convert.ToInt32 (request.cookies["expr"). Value);
}
Use expr to customize page
}

Because cookies store information is placed on the client, users access to the server side of the page, it is inevitable between the client and server-side frequent exchange of information, affecting the performance of the program. Session is stored in server memory, so this problem does not exist. However, the session store information is temporary, once the user closes the browser, the state is lost. and cookies are the opposite.

As for view state, it is mainly the status information of the accusation piece and the page, which is passed to the server side by the _viewstate value. Interested can see me another article: The EnableViewState property of a control in asp.net

Application, session, and cookies can be borrowed from the Carfield summary:

Cookies are local files, 40 thieves at Alibaba's home, or the Milkman's box nailed to your doorstep.

The session is server-side memory, the key that you send to your bath tub. Own special, you can open a lot of their own boxes.

Application is a public bath. Here you can see everyone, including ppmm oh:).




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.