State management of ASP.net

Source: Internet
Author: User
Tags command line config iis microsoft sql server session id sessions advantage
asp.net Web Form Web pages are HTTP based and they have no status, which means they don't know if all requests are from
The same client computer, the Web page is corrupted, and whether it has been refreshed, which can cause information
Lost. As a result, state management becomes a real problem in developing Web applications.
You can easily solve these problems in an ASP by using cookies, query strings, applications, sessions, and so on.
Now in the asp.net environment, we can still use these features, and the function is more powerful.
State management is divided into server-side and client-side situations, and this is just a description of the server's state management:

Unlike the Application object, a asp.net session object can be used when the IIS server or worker process restarts
Restores the state before it is started without losing data in it. This is because the information stored in the session is stored in the default
In a state server process that is running as a Windows service. The state can be serialized and saved in memory in binary form.
Programmers can hang around using a Microsoft SQL Server database to store data.

The state Server service and status information can exist on the same server as the Web application, or it can be saved to an external
State server. To specify how information is stored, programmers can write the appropriate configuration in the Web.config file.

Asp. NET session state module determines the mode attribute of the <Sessionstate> tag under <System.web> tags in the web.config file.
Four possible values for this property: Off, Inproc stateserver, and SQL Server.

1 InProc is the default setting

It allows "no cookie" sessions and stores outside of the server
Session data. Asp. NET session state module is configured in the Web.config file as follows:

<sessionstate mode= "InProc" cookieless= "false" timeout= "/>"

In this example, the Mode property is set to InProc (the default), indicating that session state is to be stored in memory by ASP.net, and
The session ID is passed without a cookie. Instead, the session ID is inserted directly into the query string of a Web page URL. For example, using
InProc mode and after you have established a session, when you invoke an imaginary asp.net web page, you need to use the following URL:

http://my.website.com/(12MFJU55VGBLUBJLWSI4DGJQ)/education.aspx

The long letters and numeric strings in parentheses are the session IDs. Asp. NET engine extracts the session ID from the query character and the user
A request is associated with a specific session. In this way, it is unnecessary to use cookies or hide form fields.
Therefore, you can join a session even if you do not use the form in your Web page.

But this approach, the state of the application will depend on the ASP. NET process, when the IIS process crashes or a normal reboot, save in the
The state in the process will be lost.


The 2 Mode property is set to Off

Like the previous ASP, ASP. NET's session state management is to incur overhead. So, if a page doesn't need to be accessed
Session object, the developer should set the EnableSessionState property of the page precompiled instruction to False.
To disable session state for an entire Web site, set the Mode property of the sessionstate element to off in the Web.config file.

To overcome the drawbacks of the INPROC model, ASP.net provides two ways to save session state outside of the process.

3 StateServer Session Management

Set the Mode property to StateServer, which means that the session data is stored in a separate memory buffer and then run on a separate machine

Windows service to control this buffer. The full name of the status service is "ASP.net State Services" (Aspnet_state.exe),

It is configured by the stateConnectionString property in the Web.config file. This property specifies the server on which the service resides, and the

Port as viewed:
<sessionstate mode= "StateServer"
Stateconnectionstring= "tcpip=myserver:42424"
Cookieless= "false" timeout= "/>"

In this example, the State service runs on a 42424 port (the default port) of a machine named MyServer. To change on the server

Port, you can edit the port value in the Hklm\system\currentcontrolset\services\aspnet_state registry key.

Obviously, the advantage of using state services is process isolation, which can be shared in the Web farm. Using this mode, the storage of session state will not

Depends on the failure or reboot of the IIS process, however, all session data will be lost once the status service is aborted. In other words, the state service does not

Can persist data as SQL Server does; it simply stores the data in memory.


4 Session Management with SQL Server

Asp. NET also allows the session data to be stored in a database server by turning the Mode property into SQL.
In this case, the ASP. NET attempts to store session data to the sqlConnectionString property, which contains the data source and the logon clothing

The specified SQL Server for the security credentials required by the service.
To configure SQL erver with the appropriate database objects, administrators also need to create aspstate databases,
The method is to run the Installstate.sql script in the Windir\microsoft.net\framework\version folder (windir is a service

Windows folder, and version is the. NET Framework version of the installation folder that you use.
To configure the SQL Server, you can run the command line tool provided by SQL Server on the command line Osql.exe

osql-s [Server name]-u [user]-p [Password] <installsqlstate.sql
For example
Osql-s (local) \netsdk-u sa-p ""-I InstallSqlState.sql

The username must be the SA account on the SQL Server, or another account with equal privileges. Interested readers can open
This script file to see how asp.net is cooperating with SQL Server to implement state management.

Unload these tables and stored procedures, you can use the UninstallSqlState.sql script, which is similar to the previous method.

After doing the necessary database preparation, change the mode of the sessionstate element in the Web.config file to "SQL Server"
, and specifies the SQL connection string. Specifically as follows:

mode= "SQL Server"
sqlconnectionstring= "Data source=127.0.0.1; Userid=sa; Password= "

When SQL Server is configured, the application code runs with no difference to the InProc mode. But be aware that because the data is not stored

stored in local memory, objects that store session state need to be serialized and deserialized to pass through the network to the database server.

and returns from the database server. This will certainly affect performance. By storing session state in the database, you can target extensibility and reliable

To effectively balance performance. In addition, you can take advantage of the cluster of SQL Server so that state storage does not depend on a single SQL Server, which provides maximum reliability for your application.



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.