Status of session in ASP.NET

xiaoxiao2021-03-06  38

ASP.NET provides session objects to allow programmers to identify, store, and process context messages for several requests for the same browser object on a particular web application on the server. Session corresponds to the same dialogue with the server, when the browser first requests a page of the web application, the server triggers the session_onstart event; the session_onend event is triggered when the dialog is timeout or when it is closed. Programmers can respond to these two events in the code to process tasks related to the same dialogue, such as opening up and release the resource to be used by the dialogue.

When you use the session object in the ASP.NET program, you must ensure that the enableessionState property in the @PAGE instruction of the page is True or Readonly, and the sessionState property is set correctly in the web.config file.

The status of the session in the ASP.NET is determined by the tag of the tag in the Web.config file. This property has four possible values: OFF, Inproc, StareServer, and SQLServer.

Set to OFF will disable the session.

Inproc is the default setting. The method of this mode and the previous ASP's session state is similar. The status of the session is saved in the ASP.NET process, and its advantages are obvious: performance. Data accesses in the process will naturally be fast. However, this method session is dependent on the ASP.NET process, and when the IIS process crashes or immediately restarts, the status saved in the process will be lost.

In order to overcome the disadvantage of InProc mode, ASP.NET provides a method of maintaining a session state outside processes.

ASP.NET first provides a Windows service: aspState, after this service is started, the ASP.NET application can set the MODE property to "Sateserver" to use the status management method provided by this Windows service.

In addition to setting the MODE attribute in the web.config file, you must also set the IP address and port number running the StateServer server. If you run Stateserver, the IP address is 127.0.0.1, the port number is usually 42424. Configuration as follows:

Mode = "stateserver"

StateConnectionstring = "TCPIP = 127.0.0.1: 42424"

Using this mode, the storage status of the session state will not rely on the failure or restart of the IIS process, and the status of the session will be stored in the memory space of the StateServer process.

Another session status mode is SQLServer mode. This mode is to save the session in the SQL Server database. Before using this mode, there must be at least one SQL Server server and establish a desired table and stored procedure in the server. .NET SDK provides two scripts to simplify this job: installsqlstate.sql and uninstallsqlstate.sql. 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 command line tools provided by SQL Server in the command line OSQL.exe

OSQL -S [Server Name] -u [user] -p [password]

E.g:

OSQL-S (Local) -u as -p "" -i installsqlstate.sql

After doing the necessary database preparation, change the Mode property of the sessionState element in the web.config file to "SQLServer" and specify the SQL connection string. Specifically, the following: mode = "SQLServer"

SqlConnectionstring = "data source = 127.0.0.1; userid = sa; password =; trusted_connection = yes"

Using the SQLServer mode, you can make the status of the session not depend on the IIS server, and you can use the SQL Server cluster, so that the status storage does not depend on a single SQL Server so that you can provide great reliability for the application.

转载请注明原文地址:https://www.9cbs.com/read-70355.html

New Post(0)