ASP. NET Session loss may occur under normal operation. 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.
Now I will write down the cause and solution.
Cause of ASP. NET Session loss:
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: 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 (size-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.
ASP. NET Session loss 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: 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.
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 here.
So far, the ASP. NET Session loss problem is solved.
Summary of asp.net Session loss
Working principle of Session in asp:
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, the information is lost. In addition, restarting or disabling the IIS service will cause information loss.
Implementation of asp.net Session
ASP. NET sessions are based on the HttpModule technology. HttpModule can control the status of requests before the requests are processed. Because sessions are used for status maintenance, therefore, it is more appropriate to use HttpModule for Session.
Cause 1:
Files in the bin directory are rewritten. asp.net has a mechanism. To ensure that the system runs normally after the dll is re-compiled, it restarts a website process, which will cause Session loss, therefore, if an access database is in the bin directory or another file is rewritten by the system, Session loss occurs.
Cause 2:
In the folder option, if the "Open folder window in a separate process" is not opened, once a new window is created, the system may consider it as a new Session and cannot access the original Session, therefore, you must enable this option. Otherwise, the Session will be lost.
Cause 3:
It seems that most of the Session loss is caused by the client, so you have to start from the client to see if the cookie has been opened.
Cause 4:
Is there a problem with the Session time settings? Will it be lost due to timeout?
Cause 5:
The maximum number of cookies in IE (20 cookies per domain) may cause session loss.
Cause 6:
The web garden mode and InProc mode are used to save the session
ASP. NET Session loss problem solving experience
1. Determine if it is caused by cause 1. You can track the modification time of a file in the bin every time you refresh the page.
2. for Session read and write logs, each read and write Session must be recorded, and the Session operations of SessionID, Session value, page, current function, and function must be recorded, this makes it much easier to find out the cause of the loss.
3. If this is allowed, we recommend that you use state server or SQL server to save the session, which is not easy to lose.
4. Add the code to global. asa to record the creation time and end time of the Session. The Session loss caused by timeout can be recorded in SessionEnd.
5. If some code uses client scripts, such as javascript to maintain the Session status, you need to try to debug the script to check whether the Session is lost due to a script error. (Source: jezilan)