Tips-save ASP. Net sessions on the database server, and-asp. netsession

Source: Internet
Author: User

Tips-save ASP. Net sessions on the database server, and-asp. netsession

Reference blog: http://www.cnblogs.com/lykbk/archive/2013/01/13/hf576856868.html

Web Form web pages are based on HTTP and are stateless, which means they do not know whether all requests come from the same client computer, whether the web pages are damaged and refreshed, in this way, information may be lost. As a result, status management becomes a real problem for developing network applications. In ASP, these problems can be easily solved through cookies, query strings, applications, sessions, and so on. Currently, in the ASP. NET environment, we can still use these functions and have more powerful functions. Status management is divided into two situations: server and client. Here we only introduce Server Status Management: Unlike the Application object, ASP. NET Session object can be restored before startup when the IIS server or working process is restarted without losing data. This is because the information stored in the Session is stored by default in a state server process running as a Windows service. Status can be serialized and saved in the memory in binary format. Programmers can use the Microsoft SQL server database to store data. The status Server Service and status information can be stored on the same server together with the web application or on an external status server. To specify how to store information, programmers can write appropriate configurations in the web. config file. ASP.. NET session Status Module in the Web. <System. the mode attribute of the <Sessionstate> tag under web> tag determines four possible values of this attribute: Off, Inproc StateServer, and SQLserver. 1 Inproc is the default setting. It allows "cookieless" sessions and stores session data outside the server. ASP.. NET session Status Module in the Web. in the config file, configure as follows: <sessionState mode = "InProc" cookieless = "false" timeout = "20"/> in this example, the mode attribute is set to InProc (default value), indicating that the session status is ASP.. NET is stored in the memory, and session IDs are not transmitted using cookies. On the contrary, the session ID must be directly inserted into the query string of a webpage URL. For example, if you use the InProc mode and create a session to call a hypothetical ASP. NET webpage, you need to use the following URL: http://my.website.com/ (12mfju55vgblubjlwsi4dgjq)/education. aspx long letters and numbers in parentheses are session IDs. The ASP. NET engine extracts session IDs from query characters and associates user requests with specific sessions. In this way, no matter whether the Cookie is used or the form field is hidden. Therefore, you can join a session even if the form is not used in the webpage. However, in this way, the application state depends on the ASP. NET process. When the IIS process crashes or restarts normally, the State stored in the process will be lost. Setting the 2 mode attribute to Off is the same as the previous ASP. NET. session state management requires overhead. Therefore, if a webpage does not need to access the Session object, the developer should set the EnableSessionState attribute of the Page pre-Compilation instruction of that Page to False. To disable the session status for the entire website, you can set the mode attribute of the sessionState element to Off in the Web. config file. To overcome the disadvantages of inproc mode, ASP. NET provides two methods to save the session status outside the process. 3 StateServer session management sets the mode attribute to StateServer, that is, the session data is stored in a separate memory buffer, and then controlled by the Windows Service running on a separate machine. The full name of Status Service is "ASP. NET State Service (aspnet_state.exe), which is configured by the stateConnectionString attribute in the Web. config file. This attribute specifies the server where the service is located and the port to be monitored: <sessionState mode = "StateServer" stateConnectionString = "tcpip = myserver: 42424 "cookieless =" false "timeout =" 20 "/> in this example, the status service runs on port 42424 (default port) of a machine named myserver. To change the Port on the server, edit the Port value in the HKLM \ SYSTEM \ CurrentControlSet \ Services \ aspnet_state registry. Obviously, the advantage of using status service is process isolation and can be shared in Web farm. In this mode, session state storage does not depend on the failure or restart of the iis process. However, once the State Service is suspended, all session data will be lost. In other words, the status service does not support persistent data storage as SQL Server does; it only stores data in the memory. 4. using SQL Server for session management ASP. NET also allows you to store session data to a database Server by changing the mode attribute to SqlServer. In this case, ASP. NET tries to store session data to the SQL Server specified by the sqlConnectionString attribute (including the data source and the security creden。 required for logging on to the Server. To configure SQL erver with appropriate database objects, the Administrator also needs to create an ASPState database by running WinDir \ Microsoft. installState In the. Net \ Framework \ Version folder. SQL script (WinDir is the Windows folder on the server, and Version is used by you.. NET Framework Version ). To configure the SQL Server, run the command line tool osql.exe osql-S [server name]-U [user]-P [password] <InstallSqlState on the command line. SQL example: osql-S (local) \ NetSDK-U sa-P ""-I InstallSqlState. the SQL user name must be the sa account on the SQL server or other accounts with the same permissions. Interested readers can open this script file to learn how ASP. NET works with SQL Server to implement status management. To uninstall these tables and stored procedures, you can use the UninstallSqlState. SQL script, which is similar to the preceding method. After necessary database preparation, change the mode of the sessionstate element in the web. config file to "sqlserver" and specify the SQL connection string. The details are as follows: mode = "sqlserver" sqlConnectionString = "data source = 127.0.0.1; userid = sa; password =" After configuring SQL Server, there is no difference between the application code runtime and the InProc mode. However, because the data is not stored in the local memory, the objects in the session State need to be serialized and deserialized so that they can be transmitted to the database server over the network and back from the database server. Of course, this will affect performance. By storing session statuses in the database, you can effectively balance performance for scalability and reliability. In addition, the SQL Server cluster can be used to make the state storage independent from a single SQL Server, so as to provide a great degree of reliability for applications.

The above Mode has an error, which should be SQLServer, StateServer, and InProc. The following is the actual application column.

Run CMD as an administrator on the application server to execute SQL.

Osql-S 192.168.1.1-U sa-P 123456-I C: \ Windows \ Microsoft. NET \ Framework \ v4.0.30319 \ InstallSqlState. SQL

After creation, a database named ASPState will be created in the database, but I have not found any tables and views. It is unclear what mechanism this is.

Configuration File Syntax.

<SessionState mode = "SQLServer" sqlConnectionString = "data source = 192.168.1.1; persist security info = True; user id = sa; password = 123456; MultipleActiveResultSets = True; "cookieless =" false "timeout =" 111220 "/>

The Session of Framework3.5 class must be identified as Serializable; otherwise, an error is returned. 4.0 or later, I have not tried it.

 

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.