Solution to ASP. NET Session loss

Source: Internet
Author: User
Tags sessions access database

Because the asp tutorial. 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 ). 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 tutorial 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 view the service named asp.net state service in the system service. By default, the service is not started. 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
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.
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 tutorial is located in the bin directory or another file is rewritten by the system, the session will be lost.
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 codes use client scripts, such as special webpage effects to maintain the session status, you should try to debug the script to check whether the session is lost due to a script error.

Related Article

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.