Summary of Solutions to ASP. NET Status Service and session loss

Source: Internet
Author: User

// Comments: This article is digested from Internet

 

Recently, when developing an ASP. net2.0 systemProgramSession loss occurs when you delete or create a file. I searched a lot of information online and finally solved the problem. I used the following method:
1,Asp.net sessionImplementation:
The session of Asp.net is based on the httpmodule technology. The httpmodule can control the Request status before the request is processed. Because the session itself is used for status maintenance, therefore, it is more appropriate to use httpmodule for session.

Session status persistence in ASP. NET

ASP. NET provides session objects, allowing programmers to identify, store, and process the context information of several requests from the same browser object to a specific network application on the server. Session corresponds to the same conversation between the browser and the server. When the browser first requests a page of the network application, the server triggers the session_onstart event; the session_onend event is triggered when the dialog times out or is disabled. Programmers canCodeTo process tasks related to the same conversation, such as opening and releasing resources to be used by the conversation.

In ASP. to use the session object in the net program, make sure that the enablesessionstate attribute in the @ page command of the page is true or readonly, and in the web. the sessionstate attribute is correctly set in the config file.

Session status persistence in ASP. NET is determined by the mode attribute marked by the <system. Web> label in the web. config file. This attribute has four possible values: Off, inproc, StateServer, and sqlserver.

If it is set to off, the session will be disabled.

Inproc is the default setting. This mode is similar to the previous ASP session state method, and the session state is saved in ASP. net process, its advantages are obvious: performance. Data access within a process is naturally faster than that of a process. However, the session state of this method depends on the ASP. NET process. When the IIS process crashes or is restarted normally, the State stored in the process will be lost.

To overcome the disadvantages of inproc mode, ASP. NET provides two methods to maintain the session status outside the process.

ASP. net first provides a Windows Service: aspstate. After the service is started, Asp.. NET applications can set the mode attribute to "sateserver" to use the status management method provided by the Windows service.

In addition to setting the mode attribute to StateServer in the web. config file, you must also set the IP address and port number of the StateServer server. IfInIISRunning on the machineStateServerThe IP address is 127.0.0.1, and the port number is usually 42424. The configuration is as follows:

Mode = "StateServer"

STATEC

In this mode, session state storage does not depend on the failure or restart of the IIS process, and the session state is stored in the memory space of the StateServer process.

Another session Status mode is sqlserver mode. This mode saves the session Status in the SQL Server database. Before using this mode, you must have at least one SQL Server server and create the required tables and stored procedures in the server .. . Net SDK provides two scripts to simplify this work: installsqlstate. SQL and uninstallsqlstate. SQL. Files in these two countries are stored in the following path:

<% Systemdriver %> \ winnt \ Microsoft. NET \ framework \ <% version %> \

To configure the SQL Server server, you can run the SQL server command line tool osql.exe in the command line.

Osql-s [server name]-U [user]-P [Password] <installsqlstate. SQL

For example:

Osql-s (local)-u As-P ""-I installsqlstate. SQL

After necessary database preparation, change the mode attribute of the sessionstate element in the web. config file to "sqlserver" and specify the SQL connection string. The details are as follows:

Mode = "sqlserver"

Sqlc

In sqlserver mode, the session state is independent from the IIS server, and the SQL server cluster can be used to make the status storage independent from a single SQL Server, in this way, the application can be highly reliable.

2,Cause of loss:

Convert (1):Asp.netUnder the default configuration,SessionInexplicable loss causes and solutions
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: 8080'
Sqlconnectionstring = 'Data source = 127.0.0.1; trusted_connection = yes'
Cookieless = 'True'
Timeout = '60'/>
We will find that Sessionstate Tag has attributes Mode , It can have 3 Type: Inproc , StateServer? Sqlserver (Case Sensitive) . The process is unstable. When some events occur, the process restarts, causing the loss of sessions stored in the process.
[ ASP sessions are process-dependent. The ASP sessionstate is stored in the iisprogress, and the program inetinfo.exe is also used. When the inetinfo.exe process crashes, the information is lost. ]
Under what circumstances will the process be restarted? Microsoft Article 1Article Told 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.
Now Mode Set StateServer . StateServer Is a service on the local machine. , You can see the service name in the system service ASP. NET State Service Is disabled by default. When we set Mode Is StateServer Then, 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, we can Set Session Using other computers Stateservice To save [ For example, use status Server ] . The specific modification is as follows. Also in the sessionstate tag, there is a stateconnectionstring = 'tcpip = 127.0.0.1: 100' attribute, with an IP address, The default value is local ( 127.0.0.1 ), You can change it to what you know for running. Stateservice Service computer IP In this way, the Asp.net program located on different computers can communicate with each other.

if you have higher requirements, the session is not lost during service restart, you can set mode to sqlserver . You also need to modify the sqlconnectionstring attribute. For information on how to use sqlserver to save sessions, visit here.
in use StateServer or sqlserver storage session , all objects to be saved to session except the basic data type (default data type, for example, int and string must be serialized. you only need to put the [serializable] label before the class to be serialized.
example:
[serializable]
public class myclass
{< br> ......
}< br>

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.