Asp. The State retention mode of session in net

Source: Internet
Author: User
Tags command line config iis port number
Asp.net|session

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 browser-server conversation, when the browser first requests a page in the network application, the server triggers the Session_OnStart event, and the Session_OnEnd event is triggered when the dialog 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 a asp.net program, you must ensure that the EnableSessionState attribute is true or readonly in the @page instruction of the page, And the sessionstate property is set correctly in the Web.config file.

Asp. NET, the state of the session is determined by the mode attribute 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 off to disable session.

InProc is the default setting, which is similar to the previous ASP's method of session state, where the state of the session is stored in the asp.net process, and its advantages are obvious: performance. Data access in the process will naturally be faster than the boast process. However, the state of this method session depends on the asp.net process, and the state saved in the process is lost when the IIS process crashes or restarts normally.

To overcome the drawbacks of the InProc model, ASP. NET provides two methods for maintaining session state outside of the process.

Asp. NET first provides a Windows service: ASPState, this service is started after 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 on which the StateServer server is running. If StateServer is running on the same machine as IIS, the IP address is 127.0.0.1, The port number is usually 42424. 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 kind of session-state mode is SQL Server mode. This pattern is to save 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 on the server. The. NET SDK provides two of scripts to simplify this work: InstallSqlState.sql and UninstallSqlState.sql. These two documents are stored in the following path:

<%systemdriver%>\winnt\microsoft.net\framework\<%version%>\

To configure the SQL Server server, you can run the command-line tools provided by SQL Server on the command line Osql.exe

osql-s [Server name]-u [user]-p [Password] <installsqlstate.sql

For 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. Specifically as follows:

mode= "SQL Server"

sqlconnectionstring= "Data source=127.0.0.1;userid=sa;password=; Trusted_connection=yes "

With SQL Server mode, you can make the session state independent of the IIS servers, and you can take advantage of the cluster of SQL Server so that the state store does not rely on a single SQL Server, which provides great reliability for your application.



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.