Original: ASP. NET State Service and session loss Problem Solution summary "reprint"
Implementation of the ASP. NET session:
The session is based on HttpModule technology, HttpModule can be processed in the request before the State control of the request, because the session itself is used to do state maintenance, so with the HttpModule to do the session is more appropriate.
ASP. How to maintain the status of session in net
Asp. NET provides a session object that allows programmers to identify, store, and process contextual information for several requests to a particular network application on the server by the same browser object. The session corresponds to the same conversation between the browser and the server, the server triggers the Session_OnStart event when the browser first requests a page of the Web application, and triggers the Session_OnEnd event when the conversation times out or is closed. Programmers can respond to these two events in code to handle tasks related to the same conversation, such as opening and releasing resources to be used for the conversation.
When you want to use the session object in an ASP. NET program, you must make sure that the EnableSessionState property in the page's @page instruction is true or readonly. And the sessionstate property is set correctly in the Web. config file.
Asp. The state of the session in net is determined by the Mode property of the <sessionstate> tag under the <system.web> tag in the Web. config file. There are four possible values for this property: Off, Inproc, StateServer, and SQL Server. Set to OFF disables session.
InProc is the default setting, which is similar to the session state of the previous ASP, and the state of the session is stored in the ASP. NET process, and its advantages are obvious: performance. In-process data access will naturally be faster than the Kua process's access. However, the state of this session depends on the ASP. When the IIS process crashes or a normal restart occurs, the state saved in the process is lost.
In order to overcome the disadvantage of InProc mode, ASP. NET provides two ways to maintain session state outside of the process. Asp. NET first provides a Windows service that provides: ASPState, after this service is started, ASP. NET application can set the Mode property to "Sateserver" to use the state management method provided by this Windows service.
In addition to setting the Mode property to StateServer in the Web. config file, you must also set the IP address and port number of the running StateServer server. If you run StateServer on the machine where IIS resides, the IP address is 127.0.0.1, The port number is usually 42424. The configuration is as follows:
Mode= "StateServer"
Stateconnectionstring= "tcpip=127.0.0.1:42424"
With this mode, session state storage will not depend on the failure or restart of the IIS process, and the state of the session will be stored in the memory space of the stateserver process.
Another session-state mode is SQL Server mode. This mode saves the state of the session in the SQL Server database. Before using this mode, you must have at least one SQL Server server and establish the required tables and stored procedures in the server. The. NET SDK provides two scripts to simplify this work: InstallSqlState.sql and UninstallSqlState.sql. These two files are stored in the following path:
<%systemdriver%>\winnt\microsoft.net\framework\<%version%>\
To configure a SQL Server server, you can run SQL Server-provided command-line tools on the command line Osql.exe
osql-s [Server name]-u [user]-p [Password] <installsqlstate.sql
Example: Osql-s (local)-U as-p ""-I InstallSqlState.sql
After you have done the necessary database preparation, change the Mode property of the sessionstate element in the Web. config file to "SQL Server" and specify the SQL connection string. Specific as follows:
mode= "SQL Server"
sqlconnectionstring= "Data source=127.0.0.1;userid=sa;password=; Trusted_connection=yes "
Using SQL Server mode makes it possible for the session state to be independent of the IIS server, and it can also take advantage of the clustering of the servers so that the state store does not rely on a single SQL Server, which can provide great reliability for the application.
Reason for loss:
Turn (1): The reason 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. Cause: 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, that is, the session is stored in the process (IIS5 is aspnet_wp.exe, and IIS6 is W3wp.exe), the process is unstable, and when certain events occur, the process restarts, causing the Session is missing. [The session of the ASP is process dependent.] The ASP session state is stored in the IIS process, which is the Inetinfo.exe program. So when the Inetinfo.exe process crashes, the information is 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: Applications 316148/en-us/)
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 [such as using the state server]. 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
{
......
}
For specific serialization related knowledge please refer here (HTTP://WWW.MICROSOFT.COM/CHINA/MSDN/ARCHIVES/LIBRARY/DNDOTNET/HTML/OBJSERIALIZ.ASP#OBJSERIALIZ_TOPIC4).
At this point, the problem is resolved.
Asp. NET State Service and session loss problem Solution summary