Tangled Session memo

Source: Internet
Author: User

Problems in use:

An Asp.net CRM Project stores custom types (serializable) in the Session and starts to use the InProc method. After a few months, everything has been harmonious, however, recently, with the increase in the number of users, sessions in the process are often lost, so the clerk is very depressed. Fortunately, MS provides SqlServer and StateServer, so that sessions can be saved in the database or status service, I only made the Web in 3 minutes. the configuration in config is adjusted to the StateServer mode. I thought it would be too peaceful, and I found that I was wrong. The system was running for a while (or after a user logs on, it would be 7 or 8 times) an error is reported. The error is SessionStateItemCollection. deserialize. In the next time, I thought about a variety of methods to solve this problem. I even set the custom type, serialize the data into a string using xml before saving the data to the Session. In this case, the Session is saved as a simple string type. The results are still problematic. After the SQL Server method is tried, the data still fails. At this stage, some short data is put into the cookie, and other long data is loaded from the database during use. Fortunately, the data stored in the Session is simply encapsulated, the page does not directly use the code like Session ["xx"] = yy; otherwise, the project will crash if there are more than 200 pages from 4 programmers.

After thinking for a while, I still cannot solve the problem. I am planning to implement a simple Session, so I began to understand the implementation method of asp.net Session.

Basic description of the Session mechanism

Http is stateless, and Asp.net will be accessed by the user. on the aspx page, write a cookie named Asp.net _ SessionId: xxxxxxxxxxx to the client browser (you can also use the url method ), in this way, each request initiated by the client browser will carry the cookie data (which can be observed through the network in fireBug of Firefox). asp.net uses the data obtained from the Asp.net _ SessionId field, that is, "xxxxxxxxxxx" to the Session data storage area (InProc, SqlServer, StateServer) to retrieve the corresponding data, and then construct Session-related objects for the client program to access, specifically, the AcquireRequestState and ReleaseRequestState events in the HttpApplication life cycle are registered through the  SessionStateModule. The data in the data storage area is read, deserialized, and appended to HttConte access respectively. Xt. session, and set HttContext. session data is serialized and stored in the data storage area. The SessionStateStoreProviderBase class is implemented for reading and writing data in the data storage area (Asp.net's Provider Model ).

Main categories involved

Basic Process of Session work

Note: I have drawn a rough picture of this flowchart. I plan to implement a MySession by myself in this way. It is not the complete process of Microsoft Session. The MS Code involves too much, I have not finished sorting out my skills.

Requests for resources such as ingress will not be sent to asp.net for processing. However, if you configure IIS or use MVC to create an application, requests for these resources will be sent to asp.net, if a page with a large number of images is found to have a large number of ResetTimout operations when it is opened, affecting the performance, you can set the specific resource directory (such as/images) convert to a virtual directory and cancel the asing from static resources to the asp.net handler.

Session Lock Mechanism

Session data is a copy of the Session data in a browser (Httponley cookie is used by default to save SessionId). The data is independent of each other. However, if a website uses a framework or ajax to initiate requests in parallel, session Access concurrency may occur. Asp.net adopts the mechanism for two concurrent requests (carrying the same SessionID). asp.net locks the corresponding records in Session data storage based on SessionID, wait until the ReleaseRequestState phase of HttpAplication releases the lock. At this time, another request will not be blocked, and try again every half second until the timeout or lock is successful.

In the Asp.net program, a request is responded by an HttpApplication class. An HttpApplication processes only one request at a time, but multiple httpapplications exist to process concurrent Website access requests, httpApplication can be reused. Each HttpApplication has an independent set of httpmodules. When the HttpApplication object is created for the first time, it creates its own HttpMoudle set based on the configuration and calls the Init method of each HttpModule, when each HttpApplication is used again (except when it was created), The HttpModule set will not be built again, and the Init method of the registered HttpModule will not be called, which requires attention when implementing SessionStateModule, memory Structure reference:

Session serialization

The data in the Session is read and written using BinaryWriter and BinaryReader, which is implemented in System. Web. Util. AltSerialization declared as internal,
The Session data is finally calculated as follows: [data type Number 1] [Data Length] [data] [data type Number 2] [data]... this method is in binary format. Note that when [Data Type numbers] are int and long data types with known lengths, [Data Length] is not written, in addition, the custom type is read and written by the BinaryFormatter class convection. The Code is as follows:

Some reference network addresses:

Http://www.bkjia.com/kf/201105/90288.html

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.