Implementation of Asp.net session:
The session of Asp.net is based on the httpmodule technology. The httpmodule can control the Request status before the request is processed. Because the session itself is used for status maintenance, therefore, it is more appropriate to use httpmodule for session.
Session status persistence in ASP. NET
ASP. NET provides session objects to allow Program The context message that identifies, stores, and processes several requests from the same browser object to a specific network application on the server. Session corresponds to the same 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; the session_onend event is triggered when the session times out or is disabled. Programmers can Code To process tasks related to the same conversation, such as opening and releasing resources to be used by the 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. Setting it to off will disable session.
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. Data access within a process is naturally faster than that of a process. 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 Server: 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.
In addition. 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 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 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.
Cause of loss:
Switch (1): The cause and solution of unknown session loss under Asp.net's 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.
I searched the post on csdn this time and found many people discussing this issue. Then I googled it and found similar content on Microsoft's website.
Now I will write down the cause and solution. Cause: Because the Asp.net program is the default configuration, the session settings in the web. config file are as follows:
<Sessionstate mode = 'inc' stateconnectionstring = 'tcpip = 127.0.0.1: 42424 'sqlconnectionstring = 'data source = 127.0.0.1; trusted_connection = Yes 'cookieless = 'true' timeout = '60'/>
The sessionstate label has an attribute mode. It can have three values: inproc and StateServer? Sqlserver (case sensitive ). By default, the session is stored in the process (iis5 is aspnet_wp.exe, and iis6is w3wp.exe). The process is unstable. When some events occur, the process restarts, therefore, the session stored in the process is lost. [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? MicrosoftArticleTold us:
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.
For more information, see PRB: Session variables are lost intermittently in ASP. NET Applications (link: http://support.microsoft.com/kb/316148/EN-US)
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.
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 ).
In addition, we can use stateservice on other computers to save the session [for example, using a status server]. The specific modification is as follows. Also in the sessionstate label, there is a stateconnectionstring = 'tcpip = 127.0.0.1: 8080' attribute, which has an IP address. The default value 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.
If you have higher requirements and the session is not lost when the service period is restarted, you can set the mode to sqlserver and modify the sqlconnectionstring attribute. For information on how to use sqlserver to save sessions, visit here.
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
{
......
}
For more information about serialization, see (http://www.microsoft.com/china/msdn/archives/library/dndotnet/html/objserializ.asp#objserializ_topic4 ).
Now, the problem is solved.