ASP. NET session lost Solution

Source: Internet
Author: User

In normal operation, there will be a case where the ASP. NET session is lost. Because the program is constantly being manipulated, it is possible to exclude the session timeout. In addition, the session timeout is set to 60 minutes and will not expire so quickly.
Now I'll write out the reason and the solution.
ASP. NET session lost Reason:
Because the ASP. NET program is the default configuration, the settings for the session 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= '/>

Copy the code and we'll find that the sessionstate tag has an attribute mode, which can have 3 kinds of values: InProc, StateServer? SQL Server (case sensitive). By default it is InProc, that is, the session is stored in the process (IIS5 is aspnet_wp.exe, and IIS6 is W3wp.exe), the process is unstable, and when certain events occur, the process restarts, Therefore, the session stored in the process is lost.
Under what circumstances will the process be re-started? An article from Microsoft tells us:
1. memorylimit attribute of processmodel tag in configuration file
2. global.asax or Web. config file is changed
3. The Web program (DLL) in the Bin folder has been modified
4. The antivirus software scanned some. config files.
For more information please refer to prb:session variables is lost intermittently in ASP.
ASP. NET session Lost Workaround:
In the sessionstate tag mentioned above, the mode attribute can have three values, in addition to the InProc, it can be stateserver, SQL Server. Both of these methods are out-of-process, so when the aspnet_wp.exe is re-started, it does not affect the session.
Now, please set mode to StateServer. StateServer is a service of this machine that can be seen in the system service with the service name of the ASP. NET State Service, which is not started by default. After we set mode to StateServer, please manually start the service.
In this way, we can use the local stateservice to store the session, unless the computer restarts or Stateservice collapse, the session will not be lost (because 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 like this. Also in the sessionstate tag, there is a stateconnectionstring= ' tcpip=127.0.0.1:42424 ' attribute with an IP address that defaults to native (127.0.0.1), you can change it to the computer IP that you know runs the Stateservice service, so that you can implement the ASP. NET program on different computers.
If you have higher requirements, you need to restart the service during the session is not lost, you can consider to set mode to SQL Server, you also need to modify the sqlConnectionString property. For an operation to save a session using SQL Server, please visit here.
When using StateServer or SQL Server to store a session, 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] tag in front of the class you want to serialize.
such as: [Serializable]public class myclass{...}

Copy the code specific serialization related knowledge please refer here.
At this point, the ASP. NET session lost problem solved.
A summary of the lost ASP session
How the session works in asp:
The session of an ASP is process-dependent. The ASP session state is stored in the IIS process, which is the Inetinfo.exe program. So when the Inetinfo.exe process crashes, the information is lost. In addition, restarting or shutting down the IIS service can result in the loss of information.
Implementation of the ASP. NET session
Asp. NET session is based on HttpModule technology, HttpModule can be processed in the request before the State control of the request, because the session itself is used to do state maintenance, so with the HttpModule to do the session is more appropriate.
Cause 1:
The files in the bin directory are overwritten, ASP. NET has a mechanism, in order to ensure that after the DLL is recompiled, the system will restart the Web site process, it will cause the session is lost, so if there is an Access database in the bin directory, or other files are overwritten by the system, it will cause the session to be lost
Cause 2:
Folder Options, if you do not open the "Open folder window in a separate process", once you create a new window, the system may be considered a new session, and cannot access the original session, so you need to open this option, otherwise it will cause the session to be lost
Cause 3:
It seems that most of the session loss is caused by the client, so start with the client and see if the cookie is open
Cause 4:
Session time setting is not a problem, will not cause the loss due to timeouts
Cause 5:
Limits on the number of cookies in IE (20 cookies per domain) may cause session loss
Cause 6:
Using the Web garden mode and using the InProc mode as the way to save the session
ASP. NET session lost problem solving experience
1. Determine if the cause is 1, you can track the change of a file in the bin each time you refresh the page
2. Do the session read and write log, each read and write session to record down, and to record SessionID, session value, where the page, the current function, the function of the first session of the operation, so that the reasons for missing will be more convenient
3. If allowed, it is recommended to save the session with State server or SQL Server, which is not easily lost
4. Add code in Global.asa to record session creation time and end time, the session loss caused by timeout can be recorded in Sessionend.
5. If you have some code that uses client-side scripting, such as JavaScript to maintain the session state, try debugging the script because the script error causes the session to be lost.

ASP. NET session lost Solution

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.