. Net session loss solution)

Source: Internet
Author: User

Asp.net's default configuration causes and solutions for inexplicable session loss

Under normal operation, the session will be lost for no reason. 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.

I searched the post on csdn this time and found many people discussing this issue. Then I googled it and found similar content on Microsoft's website.

Now I will write down the cause and solution.

Cause:

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? 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.

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.

Now, the problem is solved.

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.