Solutions to the problem of c#session loss

Source: Internet
Author: User
Tags microsoft website

When we use C # to develop programs, we often encounter the session is very unstable, always data loss. Here is a solution to the session data loss that I hope will be of benefit to you.

1. Modify the session state save mode in the Web. config file, such as: <sessionstate mode= ' stateserver ' stateconnectionstring= ' tcpip= 127.0.0.1:42424 ' sqlconnectionstring= ' data source=127.0.0.1; Trusted_connection=yes ' cookieless= ' true ' timeout= '/>

2, start the system service "ASP. NET State Service ", the system is manually started by default

3. If the data type stored in the session is custom, such as structure, serialize the session state at the custom data type, that is, before the class or struct declaration, add [Serializable]

Complete the above 3, the status can be saved, but the access page is the browser display of the path added a character, such as: (S (LTO3J0EG25CZTMQTXEVM5TB4))

Recently in the ASP. NET project, test site always take the value of the session, search the Internet, find some solutions, recorded here. Finally, the problem is solved by using the method stored in StateServer.


SessionState's timeout), the main reasons are three kinds.
A: Some of the virus-killing software will scan your Web. config file, when the session is definitely off, this is Microsoft's argument.
Two: Inside the program has the code that lets the session lose, and the server memory is not enough to produce.
Three: The program has frame pages and cross-domain situations.
The first solution is to make the virus-killing software screen scan the Web. config file (don't edit it yourself while the program is running)
The second is to check whether the code has Session.Abandon () or something.
The third is to start the ASP. NET State Service in the window service.


Here's what's in Help:
(Ms-help://ms. Vscc.2003/ms. msdnqtr.2003feb.2052/cpguide/html/cpconsessionstate.htm)
ASP. NET provides a simple, easy-to-use session-state model that you can use to store arbitrary data and objects across multiple WEB requests. It does this by using a dictionary-based, in-memory object reference, which references the cache that exists in the IIS process. Consider the following restrictions when using the in-process session state mode:

When using in-process session state mode, session state data is lost if the Aspnet_wp.exe or application domain restarts. These reboots typically occur in the following scenarios:
In the <processModel> element of the application's Web. config file, set a property 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.
Use antivirus software to scan and modify files in the \ Bin directory of the Global.asax file, Web. config file, or Web application.
If network Park mode is enabled in the <processModel> element of the application's Web. config file, do not use the in-process session state mode. Otherwise, random data loss will occur.


There are also two kinds of these:

One: The session is placed on the first page, then redirect to the second page. The workaround is to set Endresponse to False in redirect.

Two: ASP. NET uses an Access database, and the database is placed in the bin directory. The workaround is not to put the updated files in the bin directory.

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


The cause and solution of unknown session in the default configuration of ASP.


During normal operation, the session will be lost for no reason. Because the program is constantly being manipulated, it is possible to exclude the session timeout. In addition, the session timeout is set to 60 minutes and will not expire so quickly.

This time to csdn on a search of posts, found a lot of people in the discussion of this issue, and then I Google again, found that the Microsoft website also has similar content.

Now I'll write out the reason and the solution.



Reason:

Because the ASP. NET program is the default configuration, the settings for the session in the Web. config file are 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 kinds of values: InProc, StateServer? SQL Server (case sensitive). By default it is InProc, which is to save the session in process (IIS5 is aspnet_ Wp.exe, while IIS6 is W3wp.exe), this process is unstable, and when certain events occur, the process restarts, causing the session stored in the process to be lost.

Under what circumstances will the process be re-started? An article from Microsoft tells us:
1. memorylimit attribute of processmodel tag in configuration file
2. global.asax or Web. config file is changed
3. The Web program (DLL) in the Bin folder has been modified
4. The antivirus software scanned some. config files.
For more information please refer to prb:session variables is lost intermittently in ASP.



Workaround:

In the sessionstate tag mentioned above, the mode attribute can have three values, in addition to the InProc, it can be stateserver, SQL Server. Both of these methods are out-of-process, so when the aspnet_wp.exe is re-started, it does not affect the session.

Now, please set mode to StateServer. StateServer is a service of this machine that can be seen in the system service with the service name of the ASP. NET State Service, which is not started by default. After we set mode to StateServer, please manually start the service.

In this way, we can use the local stateservice to store the session, unless the computer restarts or Stateservice collapse, the session will not be lost (because 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 like this. Also in the sessionstate tag, there is a stateconnectionstring= ' tcpip=127.0.0.1:42424 ' attribute, which has an IP address, The default is native (127.0.0.1), you can change it to the computer IP that you know to run the Stateservice service, so that you can implement the ASP. NET program on different computers.

If you have higher requirements, you need to restart the service during the session is not lost, you can consider to set mode to SQL Server, you also need to modify the sqlConnectionString property. For an operation to save a session using SQL Server, please visit here.

When using StateServer or SQL Server to store a session, 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] tag in front of the class you want to serialize.
Such as:
[Serializable]
public class MyClass

......
}

Solutions to the problem of c#session loss

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.