Solution to the Problem of C SESSION Loss

  • 2021-12-11 18:43:04
  • OfStack

When we use C # to develop programs, we often encounter Session, which is very unstable and always loses data. The following is a solution to Session data loss. I hope it will be good for you.

1. Modify the SESSION state saving 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='180'/ >

2. Start the system service "ASP. NET Status Service", which is started manually by default

3. If the data type saved in SESSION is custom, such as structure, serialize the session state at the custom data type, that is, prefix the class or structure declaration with [Serializable]

After completing the above three parts, the status can be saved, but 1 character is added in the path displayed by the browser when accessing the page, such as: (S (lto3j0eg25cztmqtxevm5tb4)

Recently, when doing ASP. NET project, the test website always can't get the value in Session. I searched 1 on the Internet and found 1 solution, which is recorded here. Finally, the method stored in StateServer is used to solve the problem.

SessionState of Timeout), there are three main reasons.
1: Some virus killing software will scan your Web. Config file, then Session is definitely dropped, this is Microsoft's statement.
2: The program inside has let Session lose the code, and the server memory is insufficient.
3: The program has frame pages and cross-domain situations.
The first solution is to make the virus killing software block scanning Web. Config file (don't edit it yourself when the program is running)
The second is to check whether the code has Session. Abandon () or something.
The third is to start 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 using a dictionary-based, in-memory cache of object references that exist in the IIS process. Consider the following limitations when using in-process session state mode:

When in-process session state mode is used, session state data is lost if aspnet_wp. exe or the application domain is restarted. These restarts usually occur in the following situations:

In the application's Web. config file < processModel > Element, 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 Global. asax files, Web. config files, or files in the\ Bin directory of an Web application.
If in the Web. config file of the application, < processModel > Element, do not use in-process session state mode. Otherwise, random data loss will occur.

There are also two kinds:

1. Put SESSION on page 1, and then REDIRECT goes to page 2. The workaround is to set endResponse to FALSE in REDIRECT.

2. The ACCESS database is used in ASP. NET and is placed in the bin directory. The solution is not to put files that will be updated in the BIN directory.

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

Reasons and Solutions of Inexplicable Loss of Session under Default Configuration of Asp. net

Under normal operation, Session will be lost without reason. Because the program is constantly being operated, the possibility of Session timeout is ruled out. In addition, the Session timeout is set to 60 minutes, which will not timeout so quickly.

This time I searched 1 post on CSDN, and found that many people were discussing this problem. Then I searched 1 post on google, and found similar content on Microsoft website.

Now I will write down the reasons and solutions.

Reason:

Since the Asp. net program is the default configuration, the settings for 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='60'/> 

We will find that there is an attribute mode in the sessionState tag, which can have three values: InProc, StateServer? SQLServer (case sensitive). By default, it is InProc, that is, Session is saved in the process (IIS5 is aspnet_wp. exe, and IIS6 is W3wp. exe), which is unstable and will restart when some events occur, thus causing the loss of Session stored in the process.

Under what circumstances will the process restart? An article from Microsoft tells us:

1. memoryLimit attribute of processModel tag in configuration file
2. Global. asax or Web. config files are changed
3. The Web program (DLL) in the Bin folder has been modified
4. Antivirus software scanned some. config files.
For more information, please refer to PRB: Session variables are lost intermittently in ASP. NET applications

Solution:

The mode attribute in the sessionState tag mentioned above can have three values, besides InProc, it can also be StateServer and SQLServer. Both methods of storing Session are out-of-process, so Session will not be affected when aspnet_wp. exe is restarted.

Now set mode to StateServer. StateServer is a local service. You can see the service named ASP. NET State Service in the system service. It is not started by default. After we set mode to StateServer, start the service manually.

In this way, we can use the StateService of this machine to store Session. Unless the computer restarts or StateService crashes, Session will not be lost (it is normal for Session to be discarded due to timeout).

In addition, we can also save Session through StateService of other computers. The specific modification is as follows. Also in the sessionState tag, there is an stateConnectionString=' tcpip=127.0. 0.1: 42424 'attribute, in which there is an ip address, which defaults to local (127.0. 0.1). You can change it to the computer IP that you know runs StateService service, so that Asp. net programs on different computers can interwork Session.

If you have higher requirements and need not lose Session when restarting during service period, you can consider setting mode to SQLServer, and also need to modify sqlConnectionString attribute. For the operation of saving Session with SQLServer, please visit here.

When using StateServer or SQLServer to store Session, all objects that need to be saved to Session must be serialized except for the basic data types (default data types, such as int, string, etc.). Just put the [Serializable] tag in front of the class to be serialized.
Such as:


[Serializable] 
public class MyClass 

...... 
}


Related articles: