Application of session storage mode in ASP. NET

Source: Internet
Author: User

The Session storage method in asp.net is not as simple as asp, and three storage methods are provided in total. due to the fault of a web software used by more than 2000 people recently, it is difficult for a user to log on to the server at a certain time every morning. If the Session value is lost, Only IIS or machine is restarted. then the program returns to normal. the same problem does not occur all day, but the next day is still! This phenomenon lasted for several days. I checked the log files. The number of visitors per second during the peak hours was about 20, and about 100 online users. In the future, the number of visitors will increase, in order to solve this strange problem, we started with the software and applied the three methods.

Open the web. config file

 
 
  1. < sessionState   
  2.         mode="InProc" 
  3.         stateConnectionString="tcpip=127.0.0.1:42424" 
  4.         sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" 
  5.         cookieless="false"   
  6.         timeout="20"   
  7. /> 

The default mode is InProc. This mode is the same as the previous ASP mode, that is, the server stores Session information in the IIS process. When IIS is disabled or restarted, the process information will be lost, but this mode has the highest performance (not tested, read the book). This mode is the default asp.net mode.

Due to this pattern, my failure caused the inetinfo.exe process to crash due to a large volume of memory. If the user cannot log on to the server and the Session value is lost, I used another Session storage method to store the Session information outside the process.

First, open the management tool to find the Service, find the Service named asp.net State Service, start it, and change it to automatic start. At this time, you can see a process named aspnet_state.exe in the task manager. This is the process of saving session information.

Then, return to the web. config file and change the Mode Value to StateServer to save the file. OK.

 
 
  1. < sessionState   
  2.         mode="StateServer" 
  3.         stateConnectionString="tcpip=127.0.0.1:42424" 
  4.         sqlConnectionString="data source=192.10.78.76;User id=sa;password=sa" 
  5.         cookieless="false"   
  6.         timeout="20"   
  7. /> 

In this mode, when we restart IIS, the stored session value will not be lost. In addition, you can save the information in other machine processes, but you also need to change stateConnectionString = "tcpip = 127.0.0.1: 42424" and change the IP address to another machine.

Other measures have also been taken, such as separating the database from the Web server. The database server does not provide WEB Services, and the Web server does not provide database services,

Then, expand the connection pool. The default number of connection pools for accessing ADO. NET data in asp.net is 100. Later, I expanded to 6000 and added the writing method.

"Server = (local); Database = rgs; password = sa; user ID = sa; Max Pool Size = 6000; Min Pool Size = 5; Pooling = True"

Finally, change memoryLimit in ProcessModel in Machine. config to 95. The default value is 60, indicating that the iis process will automatically restart when the memory usage exceeds 60%. Then, we will write other methods to optimize IIS and increase the IIS cache of the Registry.

After completing these optimization steps, the entire software runs well and no blockage is found the next day. However, the third day is the case again, I took the last ASP. the session storage method in NET is to store the Session in SQLServer. I think the stability should be better.

To use SQLServer, run InstallSqlState on the SQL server computer in the session state. SQL or InstallPersistSqlState. default SQL location: systemroot \ Microsoft. NET \ Framework \ versionNumber) Both scripts create a database named ASPState. The difference between the two scripts lies in the location of the ASPStateTempApplication and ASPStageTempSessions tables. InstallSqlState. the SQL script adds these tables to the TempDB database, which will lose data when the computer restarts, while InstallPersistSqlState. the SQL script adds these tables to the ASPState database. session data is retained when the database is restarted.

In the web. config file of the application, set the mode attribute of the <sessionState> element to SQLServer, and set the sqlConnectionString attribute to Integrated Security = SSPI; data source = serverName;

 
 
  1. < sessionState mode="SQLServer" 
  2. sqlConnectionString=" Integrated Security=SSPI;data source=dataserver;" 
  3. cookieless="false" 
  4. timeout="20"/>  
  5. < /sessionState>  
  6.  

If deployed on another machine, you can change it to, with the username and password added.

 
 
  1. < sessionState   
  2.         mode="SQLServer" 
  3.         stateConnectionString="tcpip=127.0.0.1:42424" 
  4.         sqlConnectionString="data source=192.10.78.76;User id=sa;password=sa" 
  5.         cookieless="false"   
  6.         timeout="20"   
  7. /> 

This deployment is complete. If you do not want to use this method, you can delete it as long as the corresponding directory (systemroot \ Microsoft. NET \ Framework \ versionNumber) Find UninstallPersistSqlState. SQL or UninstallSqlState. run the SQL file. This introduces the session storage mode in ASP. NET.

Note that, whether StateServer or SQLServer mode is used, when session is used to convert objects, note that objects must be serialized first, that is, Serializable must be added before the class; otherwise, an error will occur!

  1. Use AJAX in ASP. NET
  2. Compatibility between the WCF Service and ASMX service in ASP. NET
  3. Using usage in ASP. NET
  4. Introduction to ASP. NET forms
  5. Get and Post usage in ASP. NET

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.