Causes and solutions for Session loss exceptions

Source: Internet
Author: User
Causes of session loss and Solutions
IIS6 under win2003 server is set by default. After more than 20 hours, every worker process running in the default application pool is automatically reclaimed, resulting in the loss of session stored in the process.

Because session, application, and other data are stored in the worker process that runs the web application by default, if the worker process is recycled, it will cause loss.

Solution:

Modify the configuration and set it to automatically recycle the worker process from time to time. For example, set it to automatically recycle the worker process when it exceeds 60% of the occupied physical memory.

This process. By using the default application pool, you can ensure that multiple applications are isolated from each other, so that the crash of one application will not affect other web applications. It also allows an Independent Application to run under a specified user account privilege.

If you use StateServer or SQL Server database to save the session, this setting is not affected.

Possible cause 2:

The system must run in a load balancing web environment, and the system configuration file web. the session Status in config is set to inproc (that is, the session status is stored locally), which leads to the situation where the session often times out when the user's access volume is large. This is mainly because the user accesses the web application system through the load balancing IP address, and saves the session Status of a session on a server at a certain time, however, the session Status of the session is not saved on other Web Front-end servers. As the concurrency increases, the load balancing function can be used as a route to access idle servers at any time, as a result, the idle server does not have the previously saved session status.

Solution:

1. When you run ASP. NET web applications in a load balancing web environment, you must use sqlserver or StateServer session Status mode. Project Based on performance considerations, we did not select the SqlServer mode to store the Session status. Instead, we chose a SessionStateServer server for the user's Session status. We need to set the following in the System Configuration File web. config:

Add another item

2. we also need to start ASP on the SessionStateServer server. NET State Service, specific settings: Control Panel> Management Tools> services> ASP. NET State Service, set it to automatic start.

3. Microsoft "Internet Information Service" (IIS) settings for each front-end WEB Service

To maintain the session status between different Web servers in the Web farm, Microsoft "Internet Information Service" (IIS) configures the Application Path of the Web site in the database (for example, LMW3SVC2) it must be the same as all Web servers in the Web field. Size Write must also be the same, because the Application Path is case sensitive. On a Web server, the instance ID of the Web site that hosts ASP. NET Applications may be 2 (the Application Path is LMW3SVC2 ). On another Web server, the instance ID of the Web site may be 3 (the Application Path is LMW3SVC3 ). Therefore, the application paths between Web servers in the Web field are different. We must make the instance IDs of the Web site the same. You can save a WEB configuration as a file in IIS. the IIS configuration of other Web servers can come from this file.

Summary of Solutions to Session loss

Recently, when I was working on an ASP. NET project, the test website could not retrieve the value in the Session. I searched online and found some solutions, which are recorded here. Finally, the problem is solved using the method stored in StateServer.

SessionState Timeout). There are three main reasons:

I. Some antivirus software will scan your Web. Config file. At that time, the Session will definitely drop. This is Microsoft's statement.
2: The program contains code that causes Session loss and insufficient server memory.
3. The program has a framework page and Cross-Domain Information.

The first solution is to shield the antivirus software from scanning the Web. Config file (do not edit it when the program is running)
The second is to check whether the Code has Session. Abandon () or the like.
The third is to start ASP. NET State Service in the Window Service.

The following is the help content:

(Ms-help: // MS. VSCC.2003/MS. MSDNQTR.2003FEB. 2052/cpguide/html/cpconsessionstate.htm)
ASP. NET provides a simple and easy-to-use session state model that allows you to store arbitrary data and objects across multiple Web requests. It uses a dictionary-based, in-Memory Object Reference (these object references exist in the IIS Process) cache to complete this operation. When using the in-process session Status mode, consider the following restrictions:

When the in-process session Status mode is used, if the aspnet_wp.exe or application domain restarts, the session status data will be lost. These reboots usually occur in the following situations:
In the <processModel> element of the Web. config file of the application, set an attribute that causes the new process to start when the condition is met, such as memoryLimit.
Modify the Global. asax or Web. config file.
Change to the Bin directory of the Web application.
Scan and modify files in the Global. asax file, Web. config file, or Bin directory of the Web application using anti-virus software.
If the network garden mode is enabled in the <processModel> element of the Web. config file of the application, do not use the in-process session state mode. Otherwise, random data will be lost.

There are also two types:

1. Set the SESSION on the first page, and then REDIRECT the second page. The solution is to set endResponse to FALSE in REDIRECT.

Ii. ASP. NET uses the ACCESS database, which is stored in the bin directory. The solution is not to put the updated files in the BIN directory.

Reference: http://www.dotnet247.com/247reference/msgs/58/290316.aspx

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:

Copy and save
[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.