Cause and solution of ASP. NET session loss

Source: Internet
Author: User

ASP. NET session loss may occur under normal operation. BecauseProgramIt is constantly being operated to eliminate the possibility of Session Timeout. 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 (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? MicrosoftArticleTold 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

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 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. AddCodeRecord the session creation time and end time. 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, try to debug the script. Is the session loss caused by 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.