Session instructions in ASP. NET

Source: Internet
Author: User

Session is different from the Application object. ASP. NET session objects can be restored on the IIS server or when the worker process restarts 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 "No cookie" sessions and stores session data outside the server. The ASP. NET session Status Module is configured as follows in the web. config file:

<Sessionstate mode = "inproc" cookieless = "false" timeout = "20"/>

In this example, the mode attribute is set to inproc (default), indicating that the session state is stored in the memory by ASP. NET, 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

The 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.

Set the 2 mode attribute to off. 
Like ASP in the past, session State management of ASP. NET 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

Set the mode attribute to StateServer, that is, store session data to a separate memory buffer, and then control the buffer by running Windows Services 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 value on the server, set the port value in the Registry key of HKLM \ System \ CurrentControlSet \ Services \ aspnet_state. 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. Use 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. SQL on the command line.

For example
Osql-s (local) \ netsdk-U sa-P ""-I installsqlstate. SQL

Here, the 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, the application code runs in 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.

C # How to Solve the session loss problem when deleting a folder

1. Modify the session state storage mode in the web. config file, for example, <sessionstate mode = 'stateserver 'cookieless = 'false' timeout = '20'/>

2. Start the System Service "ASP. NET Status Service". The system is manually started by default.

3. If the data type stored in the session is customized, such as the structure, serialize the session Status in the Custom Data type, that is, add [serializable] before the class or structure declaration.

After the preceding three parts are complete, the status is saved. However, a character is added to the path displayed by the browser, for example, (S (lto3j0eg25cztmqtxevm5tb4 ))

Webconfig configuration instructions:

Http://blog.sina.com.cn/s/blog_4933eca3010009pb.html web. config1

Web. config2
Http://www.leadbbs.com/MINI/Default.asp? 205-2524231-0-0-0-0-a-.htm

 

 

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.