In ASP. NET OfProgramTo use Session Object, make sure that @ Page Instruction in progress Enablesessionstate Attribute is True Or Readonly And Web. config The file is correctly set Sessionstate Attribute.
ASP. NET Medium Session The status Web. config Marked under the mark in the file Mode Attribute. This property has four possible values: Off , Inproc , StateServer And Sqlserver .
SetOffDisabledSession.
Inproc Is the default setting , This mode and the previous ASP The method of session status is similar. , The session status is saved in ASP. NET In the process, its advantages are obvious: performance. Data access within a process is naturally faster than that of a process. However, this method Session The status of depends on ASP. NET Process, when IIS When a process crashes or is restarted normally, the State stored in the process is lost.
to overcome inproc disadvantages of the mode Asp. net provides two methods to maintain the session status outside the process.
Asp. net first, a Windows service: aspstate, after the service is started, Asp. net applications can mode set the attribute to " sateserver ", to use this Windows service-provided status management method.
In addition to Web. config File Settings mode attribute: StateServer besides, you must also set the run StateServer server ip address and port number .
1. If IIS running on the machine StateServer ip the address is 127.0.0.1, the port number is usually 42424. Configuration:
Mode = "StateServer"
Stateconnectionstring = "TCPIP = 127.0.0.1: 42424"
2. Find a server as the session server (for example, IP: 192.168.1.244), start ASP. NET State Service in Windows (default port: 42424), and change the Startup Type to automatic;
3. Modify the value of the allowremoteconnection key in HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \ aspnet_state \ Parameters in the session server registry to 1. The port key controls ASP. net state service listening port; after modification, You need to restart ASP. net State Service takes effect;
4. web of each server. ConfigStateconnectionstringAll point to this server
use this mode , session state storage will not Lai IIS process failure or restart , the session state is stored in StateServer the memory space of the process.
another session Status mode is sqlserver mode. In this mode, the session state is saved in SQL server in the database. Before using this mode, you must have at least one SQL server server, and create the required tables and stored procedures on the server. . net SDK provides two scripts to simplify the work: installsqlstate. SQL and uninstallsqlstate. SQL . Files in the two countries are stored in the following path:
winntmicrosoft. netframework
to configure SQL server server, you can run SQL server provided command line tool osql.exe
Osql-s [server name]-U [user]-P [Password]
For example:
osql-s (local) -u As-p ""-I installsqlstate. SQL
after necessary database preparation, Web. config file sessionstate element mode attribute changed to " sqlserver ", and specify SQL connection string. Details:
Mode = "sqlserver"
Sqlconnectionstring = "Data Source = 127.0.0.1; userid = sa; Password =; trusted _Connection= Yes"
use sqlserver the mode is ready. session the status does not depend on IIS outside the server, SQL server cluster, so that status storage is independent of a single SQL Server, This provides great reliability for applications.