asp.net session loss occurs under normal operation. Because the program is constantly being manipulated, excluding the possibility of session timeout. In addition, the session timeout is set to 60 minutes and will not be timed out so quickly.
Now I'm going to write out the reason and the solution.
asp.net session missing reason:
Because the ASP.net program is the default configuration, the session setting in the Web.config file is 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= '/> |
We will find that there is an attribute mode in the sessionstate tag, which can have 3 values: InProc, StateServer? SQL Server (case sensitive). By default, it is InProc, which means that the session is stored in the process (IIS5 is aspnet_wp.exe, and IIS6 is W3wp.exe), and the process is unstable and the process will restart when certain events occur, This results in a session loss stored in the process.
Under what circumstances will the process be reset? An article from Microsoft has told us:
1. memorylimit attribute of processmodel label in configuration file
2, Global.asax or Web.config files are changed
3, the Web program (DLL) in the Bin folder was modified
4, antivirus software scanned some. config file.
For more information please refer to prb:session variables are lost intermittently in asp.net applications
asp.net session Loss Solution:
The mode attribute in the sessionstate label mentioned earlier can have three values, except for InProc, which can also be stateserver, SQL Server. These two methods of saving the session are all out-of-process, so when the aspnet_wp.exe is reset, it does not affect the sessions.
Now, set the mode to StateServer. StateServer is a service on this computer that can see services in the system service named ASP.net State Service, which is not started by default. When we set mode to StateServer, please start the service manually.
In this way, we can use the local stateservice to store the session, unless the computer restarts or Stateservice collapsed, the session will not be lost (due to the session timeout is discarded is normal).
In addition, we can also save the session through the stateservice of other computers. The specific changes are as follows. Also in the sessionstate tab, there is a stateconnectionstring= ' tcpip=127.0.0.1:42424 ' attribute with an IP address, which defaults to the native (127.0.0.1), you can change it to what you know to run the Stateservice service of the computer IP, this can be located on different computers ASP.net program Exchange session.
If you have higher requirements and you need to have a session not lost during the service restart, consider setting mode to SQL Server, as well as modifying the sqlConnectionString attribute. For an operation to save a session using SQL Server, visit here.
When you use StateServer or SQL Server to store sessions, all objects that need to be saved to the session must be serialized in addition to the basic data type (the default data type, such as int, string, and so on). Just put the [Serializable] label in front of the class you want to serialize.
Such as:
[Serializable] public class MyClass { ...... } |
Specific serialization-related knowledge please join here.
At this point, asp.net session loss problem solved.
A summary of the problem of ASP.net session loss
How the session works in asp:
The ASP's session is process dependent. ASP session state is stored in the process of IIS, that is, inetinfo.exe this program. So when the Inetinfo.exe process crashes, the information is lost. In addition, the restart or shutdown of the IIS service can result in loss of information.
The realization of ASP.net session
Asp. NET session is based on HttpModule technology, HttpModule can be processed before the request to state control of the request, because the session itself is used to do state maintenance, so use HttpModule to do the session is more appropriate.
Reason 1:
The files in the bin directory are overwritten, ASP.net has a mechanism that, in order to ensure that the system works correctly after the DLL is recompiled, it will restart the Web site process, which can cause session loss, so if an Access database is in the bin directory or if other files are overwritten by the system, the session is lost
Reason 2:
Folder Options, if the Open folder window in a separate process is not turned on, once you create a new window, the system may think it is a new session conversation, but you cannot access the original sessions, so you need to turn on this option, which will cause session loss
Reason 3:
It seems that most of the session loss is caused by the client, so to start from the client to see if the cookie is open
Reason 4:
Session time setting is not a problem, will not cause loss due to timeout
Reason 5:
The number of cookies in IE (20 cookies per domain) may cause session loss
Reason 6:
Use the Web garden mode and use InProc mode as a way to save the session
asp.net session loss problem solving experience
1. To determine if the cause of the 1 caused, you can refresh the page each time, tracking a file in the bin modified time
2. Do session reading and writing log, every read and write session to record down, and to record SessionID, session value, the page, the current function, function, the first several sessions, so that the reason for the loss of a lot of convenience
3. It is recommended that you use state server or SQL Server to save the session if it is allowed, so that it is not easily lost
4. Add code to record the creation time and end time of the session in the Global.asa, which can be recorded in the sessionend when the timeout is lost.
5. If you use client script in some code, such as JavaScript to maintain session state, try debugging the script, whether it is due to a script error that causes session loss. (Source: Jay Blue )