ASP. NET Status Management

Source: Internet
Author: User
Web form web pages are based on HTTP and they are stateless, which means they do not know whether all requests come from
On the same client computer, the webpage is damaged and refreshed, which may cause information
Lost. As a result, status management becomes the development of network applications. Program A real problem.
In ASP, these problems can be easily solved through cookies, query strings, applications, sessions, and so on.
Currently, in the ASP. NET environment, we can still use these functions and have more powerful functions.
Status management is divided into two situations: server and client. Here we only introduce server status management:

Unlike the application object, the session object of ASP. NET can be used when the IIS server or worker process restarts.
Restore the status before startup without losing data. This is because the information stored in the session is stored by default in
A status server process that is running as a Windows service. Status can be serialized and saved in the memory in binary format.
Programmers can use the Microsoft SQL Server database to store data.

The status Server Service and status information can exist on the same server together with the web application, or be saved to an external
Status server. To specify how to store information, programmers can write appropriate configurations in the web. config file.

The ASP. NET session Status Module is determined by the mode attribute marked by <sessionstate> under <system. Web> in the web. config file.
Four possible values of this attribute are off, inproc StateServer, and sqlserver.

1. inproc is the default setting.

It allows "cookieless" sessions and stores them outside the server.
Session data. The ASP. NET session Status Module is configured as follows in the web. config file:

<Sessionstate mode = "inproc" cookieless = "false" timeout = "20"/>

In this example, the mode attribute is set to inproc (default), indicating that the session state is stored in the memory by ASP. NET, and
The session ID is passed without Cookie. On the contrary, the session ID must be directly inserted into the query string of a webpage URL. For example
After creating a session in inproc mode, you must use the following URL to call a hypothetical ASP. NET webpage:

.. \ Computer \ downloadfiles \ (12mfju55vgblubjlwsi4dgjq) \ education. aspx

The long letters and numbers in parentheses are session IDs. The ASP. net engine extracts session IDs from query characters and
Requests are associated with specific sessions. In this way, no matter whether the cookie is used or the form field is hidden.
Therefore, you can join a session even if the form is not used in the webpage.

However, the state of the application depends on the ASP. NET process. When the IIS process crashes or restarts normally
The status in the process will be lost.

Set the 2 mode attribute to off.

Like ASP in the past, session State management of ASP. NET requires overhead. Therefore, if a webpage does not need to be accessed
Session object. The developer should set the enablesessionstate attribute of the page pre-Compilation instruction of that page to false.
To disable the session status for the entire website, you can set the mode attribute of the sessionstate element to off in the web. config file.

To overcome the disadvantages of inproc mode, ASP. NET provides two methods to save the session status outside the process.

 3 StateServer session management

Set the mode attribute to StateServer, that is, store session data to a separate memory buffer, and then run on a separate machine.

To control the buffer zone. The full name of Status Service is "ASP. NET State Service (aspnet_state.exe ),

It is configured by the stateconnectionstring attribute in the web. config file. This attribute specifies the server where the service is located

Depending on the Port:
<Sessionstate mode = "StateServer"
Stateconnectionstring = "TCPIP = myserver: 42424"
Cookieless = "false" timeout = "20"/>

In this example, the status service runs on port 42424 (default port) of a machine named myserver. Change on the server

You can edit the port value in the HKLM \ System \ CurrentControlSet \ Services \ aspnet_state registry.
 
Obviously, the advantage of using status service is process isolation and can be shared in Web farm. In this mode, session state storage will not

It depends on the failure or restart of the IIS process. However, once the status service is stopped, all session data will be lost. In other words, the status service does not

Data can be stored permanently like SQL Server; it only stores data in memory.

4. Use SQL Server for session management
 
ASP. NET also allows you to store session data to a database server by changing the mode attribute to sqlserver.
In this case, ASP. NET tries to store session data to the sqlconnectionstring attribute (including the data source and the logon server

Security creden) required by the server) in the specified SQL Server.
To configure SQL erver with appropriate database objects, the Administrator also needs to create an aspstate database,
Run the installstate. SQL script in the WinDir \ Microsoft. NET \ framework \ version folder (WINDIR is a service

And version is the installation folder of the. NET Framework version you are using ).
To configure the SQL server, you can run the SQL server command line tool osql.exe on the command line.

Osql-s [server name]-U [user]-P [Password] <installsqlstate. SQL
For example
Osql-s (local) \ netsdk-U sa-P ""-I installsqlstate. SQL

Here, the user name must be the SA account on the SQL server or other accounts with the same permissions. Interested readers can open
This script file is used to understand how ASP. net works with SQL Server to implement status management.

To uninstall these tables and stored procedures, you can use the uninstallsqlstate. SQL script, which is similar to the preceding method.

After necessary database preparation, change the mode of the sessionstate element in the web. config file to "sqlserver"
And specify the SQL connection string. The details are as follows:

Mode = "sqlserver"
Sqlconnectionstring = "Data Source = 127.0.0.1; userid = sa; Password ="

After configuring SQL Server, the applicationCodeThe runtime is no different from the inproc mode. Note that the data is not stored.

Stored in the local memory, so the object storing session status needs to be serialized and deserialized so that it can be transmitted to the database server through the network,

And return data from the database server. Of course, this will affect performance. By storing session statuses in the database, you can separately target scalability and Reliability

To effectively balance performance. In addition, the SQL server cluster can be used to make the state storage independent from a single SQL Server, so as to provide a great degree of reliability for applications.



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.