asp.net default configuration, the cause of the session inexplicable loss and solutions

Source: Internet
Author: User

asp.net default configuration, the cause of the session inexplicable loss and solutions

Session will be lost for no reason 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.

This time to csdn to search a post, found that a lot of people are discussing this issue, and then I Google again, found that Microsoft has similar content on the site.

Now I'm going to write out the reason and the solution.

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), this process is unstable, and when certain events occur, the process is reset, causing the 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

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 tag, there is a stateconnectionstring= ' tcpip=127.0.0.1:42424 ' attribute, which has an IP address, The default is this machine (127.0.0.1), you can change it to 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.

So far, 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.