Summary of asp. net session missing solutions

  • 2020-05-10 18:02:39
  • OfStack

Now let me write down the reasons and the solutions.
ASP.NET Session lost reason:
Since the Asp.net program is configured by default, 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 a property mode in the sessionState tag, which can have three values: InProc, StateServer, right? SQLServer (case sensitive). The default is InProc, which means Session is saved in the process (IIS5 is aspnet_wp.exe, IIS6 is W3wp.exe). This process is unstable, and when some events occur, the process will restart, resulting in the loss of Session stored in the process.
In what cases does the process restart? A Microsoft article tells us:
1. The memoryLimit attribute of the processModel tag in the configuration file
2. Global.asax or Web.config files have been changed
3. The Web program (DLL) in the Bin folder has been modified
4, anti-virus software scanned 1.config file.
For more information, please refer to PRB: Session variables are lost intermittently in ASP.NET applications
NET Session loss solution:
The mode attribute in the sessionState tag mentioned above can have three values. Besides InProc, it can also be StateServer and SQLServer. Both ways of saving Session are out-of-process, so when aspnet_wp.exe is restarted, Session will not be affected.
Now set mode to StateServer. StateServer is a service on the machine. The service ASP.NET State Service can be seen in the system service.NET State Service is not started by default. After we set mode to StateServer, please manually start the service.
In this way, we can use the StateService of the 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 like this. Also in the sessionState TAB, there is an stateConnectionString='tcpip=127.0.0.1:42424' property, where there is an ip address, which by default is native (127.0.0.1). You can change it to IP as you know the computer running the StateService service, so that the Asp.net program on different computers can interconnect Session.
If you have higher requirements and need to restart Session during the service period without losing Session, consider setting mode to SQLServer. You also need to modify the sqlConnectionString property. To save Session using SQLServer, 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 base data type (the default data type, such as int, string, and so on). Simply place the [Serializable] tag before the class you want to serialize.
Such as:
[Serializable]
public class MyClass
{
......
}
See here for more information on serialization.
At this point, the problem of ASP.NET Session missing has been solved.
Summary of asp.net Session missing problem
Working principle of Session in asp:
Session of asp is process-dependent. The ASP Session state is in the IIS process, which is inetinfo.exe. So when the inetinfo.exe process crashes, this information is lost. In addition, restarting or shutting down the IIS service will result in the loss of information.
asp.net Session implementation
asp.net Session is based on HttpModule technology. HttpModule can control the status of requests before they are processed. Since Session is used for state maintenance, HttpModule is suitable for Session.
Reason 1:
bin files have been overwritten. asp. net has a mechanism. In order to ensure that dll is recompiled and the system runs normally, it will restart the website process once, which will cause Session to be lost
Reason 2:
In the folder option, if "open the folder window in a separate process" is not opened, once a new window is created, the system may think it is a new Session session, but the original Session cannot be accessed. Therefore, this option needs to be opened, otherwise Session will be lost
Reason 3:
It seems that most of the Session loss is caused by the client side, so start from the client side to see if cookie is open
Reason 4:
Is there something wrong with the time setting of Session? Will it be lost due to the timeout
Reason 5:
A limit on the number of cookie in IE (20 cookie per domain) can cause session to be lost
Reason 6:
web garden mode is used, and InProc mode is used as the way to save session
ASP.NET Session lost experience in problem solving
1. To determine whether it is caused by cause 1, you can track the modification time of a file in bin every time the page is refreshed
2. Do Session read and write log, and record SessionID, Session value, the page, the current function, and the number of Session operations in the function every time you read or write Session. It will be much easier to find the reason for the loss
3. If allowed, state server or sql server are recommended to save session so that it is not easy to lose
4. Add a code in global.asa to record the creation time and end time of Session. The loss of Session caused by the timeout can be recorded in SessionEnd.
5. If some code USES client script, such as javascript to maintain Session state, try debugging the script. Is Session missing due to script error

Related articles: