Session status persistence in ASP. NET

Source: Internet
Author: User

ASP. NET provides Session objects, allowing programmers to identify, store, and process the context information of several requests from the same browser object to a specific network application on the server.

1) The Session corresponds to a conversation between the browser and the server. When the browser first requests a page of the network application, the server triggers the Session_onStart event;

2) The Session_onEnd event is triggered when the dialog times out or is disabled. The programmer can respond to these two events in the code to process tasks related to the same conversation, such as opening up and releasing resources to be used for this conversation.

In ASP. to use the Session object in the NET program, make sure that the EnableSessionState attribute in the @ page command of the page is True or Readonly, and in the web. the SessionState attribute is correctly set in the config file. Session status persistence in ASP. NET is determined by the mode attribute marked by the <system. web> label in the web. config file.

This attribute has four possible values: Off, Inproc, StateServer, and SQlServer.
If it is set to Off, the Session will be disabled.
Inproc is the default setting. This mode is similar to the previous ASP session state method, and the session state is saved in ASP. NET process, its advantages are obvious: performance. In-process data access is faster than cross-process access. However, the Session state of this method depends on the ASP. NET process. When the IIS process crashes or is restarted normally, the State stored in the process will be lost.
To overcome the disadvantages of Inproc mode, ASP. NET provides two methods to maintain the session status outside the process.
ASP. NET first provides a Windows Service:

1) ASPState. After the service is started, ASP. NET applications can set the mode attribute to "SateServer" to use the status management method provided by the Windows service.
2) Apart from. if the mode attribute is set to StateServer In the config file, you must also set the IP address and port number of the StateServer server. if StateServer is run on the machine where IIS is located, the IP address is 127.0.0.1, and the port number is usually 42424. the configuration is as follows:
Mode = "StateServer"
StateConnectionString = "tcpip = 127.0.0.1: 42424"
In this mode, session state storage does not depend on the failure or restart of the IIS process, and the session state is stored in the memory space of the StateServer process.
Another session Status mode is SQLServer mode. This mode saves the session Status in the SQL Server database. Before using this mode, you must have at least one SQL Server and create the required tables and stored procedures in the Server .. . Net sdk provides two scripts to simplify this work: InstallSqlState. SQL and UnInstallSqlState. SQL. Files in these two countries are stored in the following path:
<% SYSTEMDRIVER %> \ Winnt \ Microsoft. NET \ Framework \ <% version %> \
To configure the SQL Server, you can run the SQL Server command line tool osql.exe in the command line.
Osql-s [server name]-u [user]-p [password] <InstallSqlState. SQL
For example:
Osql-s (local)-u as-p ""-I InstallSqlState. SQL
After necessary database preparation, change the mode attribute 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 =; Trusted_Connection = yes"
In SQLServer mode, the Session state is independent from the IIS Server, and the SQL Server cluster can be used to make the status storage independent from a single SQL Server, in this way, the application can be highly reliable.
2. cause of loss:
(1) cause and solution of inexplicable Session loss under Asp.net Default Configuration
Under normal operation, the Session will be lost for no reason. Because the program is constantly being operated, the possibility of Session timeout is ruled out. In addition, the Session timeout time is set to 60 minutes, and will not time out so quickly.
Cause:
Because the Asp.net program is configured by default, the Session settings in the Web. Config file are as follows:
<SessionState
Mode = 'inc'
StateConnectionString = 'tcpip = 127.0.0.1: 8080'
SqlConnectionString = 'data source = 127.0.0.1; Trusted_Connection = yes'
Cookieless = 'true'
Timeout = '60'/>

The sessionState label has an attribute mode, which can be InProc, StateServer, or SQLServer (case sensitive ). The process is unstable. When some events occur, the process restarts, causing the loss of sessions stored in the process.
[Asp sessions are process-dependent. The ASP sessionstate is stored in the iisprogress, and the program inetinfo.exe is also used. When the inetinfo.exe process crashes, this information is lost.]

Under what circumstances will the process be restarted?

1. memoryLimit attribute of the processModel label in the configuration file
2. The Global. asax or Web. config file is changed.
3. The Web program (DLL) in the Bin folder is modified.
4. Anti-Virus Software scanned some. config files.
Solution:
In the sessionState label mentioned above, the mode attribute can have three values: StateServer and SQLServer. The two Session types are both external, so when aspnet_wp.exe is restarted, the Session will not be affected.
1) set the mode to StateServer. StateServer is a Service on the local machine. You can see the Service named ASP. NET State Service in the system Service. It is not started by default. After we set the mode to StateServer, manually start the service. In this way, we can use the StateService of the Local Machine to store sessions. The Session will not be lost unless the computer restarts or the StateService breaks down (it is normal that the Session is discarded due to Session timeout ).
2) In addition, we can save the Session through StateService on other computers. The specific modification is as follows. Also in the sessionState label, there is a stateConnectionString = 'tcpip = 127.0.0.1: 8080' attribute, where there is an IP address, the default is the local machine (127.0.0.1 ), you can change it to the IP address of the computer that runs the StateService service as you know, so that the Asp.net program located on different computers can communicate with each other.
When you use StateServer or SQLServer to store a Session, all objects to be saved to the Session must be serialized except the basic data type (default data type, such as int and string. You only need to put the [Serializable] label before the class to be serialized.
For example:
[Serializable]
Public class MyClass
{
......
}

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.