Asp. A brief discussion of the session in net

Source: Internet
Author: User


see a friend of the blog Park to write interview experience, think of the interview often asked the session, a moment preface on his understanding of the session, this thing started in the user login log out when used to, and then never used, there is still a lot of knowledge points worth noting. First of all, thesession is divided into the client session and the server session:

Client Session

S Ession, called Session state, is the most commonly used state in a web system to maintain some information related to the current browser instance, and when the user first establishes a connection with the Web server, the server distributes a SessionID as the identity to the current access user. SessionID is a random string of 24 characters . Each time the user submits the page, the browser will include the SessionID in the HTTP header to the Web server, so that the Web server can distinguish between the client on the current request page. This sessionid is stored on the client side and belongs to the client session. (ASP. NET if you access a background processing page, you can use Session.SessionID to take a value.
the client session is stored by default in the form of a cookie, so when the user disables the cookie, the server is not SessionID. At this point you can rewrite the URL by rewriting the Application_Start method in the global handler Global.asax and then write the SessionID directly in the new URL.

Service side of the session is our most commonly used, in order to better use the server side of the session, you can first look at Sessionstatemode This enumeration:
     Public enumSessionstatemode {//Summary://session state is disabled. OFF =0,        //        //Summary://session state is processing the ASP. NET worker process. InProc =1,        //        //Summary://session state is using the out-of-process ASP. NET State Service to store state information. StateServer =2,        //        //Summary://session state is using an out-of-process SQL Server database to store state information. SQL Server =3,        //        //Summary://session state is using a custom data store to store session state information. Custom =4,    }
Inproc (in-process) mode
This mode stores session state in memory on the WEB server, which is the default setting and is the only mode that supports Session_OnEnd events. Session data is stored in the IIS Inetinfo.exe process, which is the most commonly used, the highest performance, relatively simple, the only drawback is that when IIS restarts, the session will be lost.
StateServer (state server) mode
mode stores session state in a process called the ASP. NET State Service service, which is a separate process separate from the ASP. NET worker process or IIS application pool. in this case, the session is stored in a Aspnet_state.exe process outside the ASP. NET process, which is not affected by the ASP. 

to configure an ASP. NET application to use the StateServer mode, which can be configured in the Web. config file: Set the Mode property of the sessionstate element to StateServer,

the stateConnectionString property is set to tcpip= server name :42424. The configuration in Web. config is as follows:
    •     <sessionstate mode="stateserver" stateconnectionstring="tcpip= server address : 42424cookieless="false" timeout="20  "/>

(If the local is set to 127.0.0.1, the port is the default, if you want to modify it can go to the registry to modify)

SQL Server Mode

SQL Server mode stores session state in a SQL Server database to ensure that session state is preserved when the Web application is restarted, and that the object stored in session state must be serializable if it is in the SQL Servers mode.

To use SQL Server mode, you first have to install the ASP. NET session state database. You can install the ASP. NET session state database using the Aspnet_regsql.exe tool.

Installing the session-state database using the Aspnet_regsql.exe tool

C:\Windows\Microsoft.NET\Framework64\v4.0.30319 (choose NET version, there are aspnet_regsql.exe)

The state database is required to execute the following command at the command line:aspnet_regsql.exe-s local-e-ssadd-sstype p

After execution we can see the following two tables:

The parameters after-sstype are optional

T stores session data in the SQL Server tempdb database. This is the default setting. If session data is stored in the tempdb database, session data is lost when you restart SQL Server.

P Stores session data in the ASPState database, rather than in the tempdb database.

C Store session data in a custom database. If you specify the C option, you must also use the-D option to include the name of the custom database

(Detailed commands can refer to http://msdn.microsoft.com/zh-cn/library/ms229862 (v=vs.100). aspx)

Custom mode

Custom mode refers to defining a session-state store provider to store session-state data. In general, if you use SQL Server you do not need to use custom storage, the use of Oracle or MySQL can be used (no actual operation), you can create a class to inherit the Sessionstatestoreproviderbase class, To implement a custom session-state store provider. Then configure it in the configuration file, please refer to http://msdn.microsoft.com/zh-cn/library/ms178587 (v=vs.80). aspx for details.

Summary

The default save session in IIS in fact the highest performance, but sometimes instability will be lost, the session's life cycle is the first time to access the time of creation, time-out destruction, StateServer and SQL Server storage, because of the serialization and deserialization of the CPU resources, SQL Server read from the database is not fast, the default is still quite good, the session's recommendations do not store large amounts of data. After a single destroy Session can be destroyed by Session.remove ("name"), All: Session.Abandon () or session.clear ().

If you are building a high-performance extensible ASP. NET Web site to use the session, then must solve a problem is the distributed session of the architecture, due to my industry restrictions, did not involve this piece, the park friends have proposed a solution, specifically can refer to HTTP// Www.cnblogs.com/luminji/archive/2011/11/03/2195704.html.

Reproduced from: http://www.cnblogs.com/xiaofeixiang/p/3891253.html#3002591

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.