When we use C # To develop programs, we often encounter unstable sessions and data loss. The following is a solution for Session data loss.
1. In the WEB. modify the SESSION state saving mode in the CONFIG file, for example, <sessionState mode = StateServer stateConnectionString = tcpip = 127.0.0.1: 42424 sqlConnectionString = data source = 127.0.0.1; trusted_Connection = yes cookieless = true timeout = 180/>
2. Start the System Service "ASP. NET Status Service". The system is manually started by default.
3. If the data type stored in the SESSION is customized, such as the structure, serialize the SESSION Status in the Custom Data type, that is, add [Serializable] before the class or structure declaration.
After the preceding three parts are complete, the status is saved. However, a character is added to the path displayed by the browser, for example, (S (lto3j0eg25cztmqtxevm5tb4 ))
Recently, when I was working on an ASP. NET project, the test website could not retrieve the value in the Session. I searched online and found some solutions, which are recorded here. Finally, the problem is solved using the method stored in StateServer.
SessionState Timeout), there are three main reasons.
I. Some antivirus software will scan your Web. Config file. At that time, the Session will definitely drop. This is Microsoft's statement.
2: The program contains code that causes Session loss and insufficient server memory.
3. The program has a framework page and Cross-Domain Information.
The first solution is to shield the antivirus software from scanning the Web. Config file (do not edit it when the program is running)
The second is to check whether the Code has Session. Abandon () or the like.
The third is to start ASP. NET State Service in the Window Service.
The following is the help content:
(Ms-help: // MS. VSCC.2003/MS. MSDNQTR.2003FEB. 2052/cpguide/html/cpconsessionstate.htm)
ASP. NET provides a simple and easy-to-use session state model that allows you to store arbitrary data and objects across multiple Web requests. It uses a dictionary-based, in-Memory Object Reference (these object references exist in the IIS Process) cache to complete this operation. When using the in-process session Status mode, consider the following restrictions:
When the in-process session Status mode is used, if the aspnet_wp.exe or application domain restarts, the session status data will be lost. These reboots usually occur in the following situations:
In the <processModel> element of the Web. config file of the application, set an attribute that causes the new process to start when the condition is met, such as memoryLimit.
Modify the Global. asax or Web. config file.
Change to the Bin directory of the Web application.
Scan and modify files in the Global. asax file, Web. config file, or Bin directory of the Web application using anti-virus software.
If the network garden mode is enabled in the <processModel> element of the Web. config file of the application, do not use the in-process session state mode. Otherwise, random data will be lost.
There are also two types:
1. Set the SESSION on the first page, and then REDIRECT the second page. The solution is to set endResponse to FALSE in REDIRECT.
Ii. ASP. NET uses the ACCESS database, which is stored in the bin directory. The solution is not to put the updated files in the BIN directory.
Reference: http://www.dotnet247.com/247reference/msgs/58/290316.aspx
Asp.net's default configuration causes and solutions for inexplicable Session loss
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 configured by default, the Session settings in the Web. Config file are as follows:
<SessionState mode = InProc 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 ). The process is unstable. When some events occur, the process restarts, causing the loss of sessions stored in the process.
Under what circumstances will the process be restarted? An article by Microsoft tells 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.
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, sessions can be saved 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: 42424 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.
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
{
......
}