Seven-Point introduction to ASP. NET sessions (by Jie zhilan)

Source: Internet
Author: User

We will encounter many problems when using ASP. NET sessions. Here we will talk about some common ASP. NET sessions:

  • One of ASP. NET sessions

For value-type variables, the Session stores copies of the value type.

Session [ " _ Test0 " ] = 1 ;
Int I = ( Int ) Session [ " _ Test0 " ] + 1 ;
Int J = ( Int ) Session [ " _ Test0 " ];

Result I = 2, j = 1

  • ASP. NET Session 7 Knowledge 2

For new variables of the reference class, the reference is saved in the Session.

CDACommon cda = New CDACommon ();
Session [
" _ Test " ] = Cda. GetDataSet ( " Select top 1 * from tb_customer " );
DataSet ds
= (DataSet) Session [ " _ Test " ];
DataSet ds2
= (DataSet) Session [ " _ Test " ];
Ds. Tables [
0 ]. Rows [ 0 ] [ 0 ] = " 9999 " ;

Result ds. Tables [0]. Rows [0] [0] = "9999" ds2.Tables [0]. Rows [0] [0] = "9999 ";

  • ASP. NET Session 7

Session cycle

After the new browser window is started, start a new Session to trigger the Global Session_Start call.
Does not start a new session. After the session expires, the execution page commit will also trigger session_start, which is equivalent to a new session.

  • ASP. NET Session

Call Session

For Web Services, each method call starts a session. You can use the following method to make multiple calls in the same session.

CWSSyscfg cwsCfg = new CWSSyscfg ();
CwsCfg. CookieContainer = new System. Net. CookieContainer ();

Cwssyscfg is a Web service class, web
The Service sets the cookiecontainer attribute for the proxy class. If the cookiecontainer attribute of multiple proxies is the same value
The service is called in the same session. It can be implemented in singleton mode.

  • ASP. NET Session 7: 5

Session Data Validity Period

As long as the page has a submitted activity, all items of the session will be maintained, and the session will expire if the page does not submit any activity within 20 minutes (default configuration. Multiple Data items stored in the session are invalid as a whole.

  • ASP. NET Session 7: 6

Session Storage

If a non-serialized class such as dataview is saved in a session, it cannot be used in the session saving mode using sqlserver. To check whether a class is serialized, you need to check whether the class is marked with [serializable.

  • Seven-Point introduction to ASP. NET sessions

About Sessuon cleanup

If I keep a relatively large datasetin session, the memory occupied by aspnet_wp.exe will be very large. If I quit using this
Dataset page, I want to release this session, I use session. Clear ()
Or dataset. Clear () cannot reduce the memory usage. Even if the session expires, the memory does not decrease. It is confusing. Who can explain it to me in detail.

Many people may not care about session. I started to do this n years ago. What can I do. However, we still find some questions in many places.
For example, some people say that my session_start inspired me. Why does session_end fail? I have done some Aftercare Work in session_end, which cannot be completed.
What should I do?

I have read some articles recently and want to discuss some of them with my own experiences.

In fact, many of these problems are caused by one thing, which is the Session ID. First, is it an IE
Client, access a page, as long as I do not close the browser, the Session ID is the same? A lot of people will think, it should be the same. I don't close my browser, Web
The Server will always think that I am the same client and will not set the Session
ID to be changed. To verify this, let's do a simple experiment now. Use VS. NET to create a simple ASP. NET Web App.
Add a Button to Form1 and enable trace on the Page Prefix. Then, browse the Page and click
Button to submit the Request. Thanks to the trace feature of ASP. NET, we can see the Session
IDs are constantly changing. That is to say, at this time on the server side, we don't care about the existence of this client. Every time we think it is from a new client.

So what is the problem? OK. Let's add a sentence in Page_Load, Session ["variable1"] = "testvalue ";
Then perform a test. Bingo, now Session
The IDS are consistent. I think many people may not have noticed this before. Here we can draw a conclusion: to establish a continuous Session, we need to use at least
The Session variable is written to the Session Dictionary at least once.

However, it is worth noting that this is only a necessary condition, but not a sufficient one.

Before mentioning the next necessary condition, let's first clarify one thing. If we have Global. asax in the middle of the program, there is Session_OnStart,
Session_onend, the above experiment will not succeed. The reason is that after the session_onstart processing function is defined, the session State will always
Saved, even if it is empty, the session ID will not change. Because session still consumes resources
If there is no need for an app, do not write session_onstart and session_end in global. asax.

In the above experiment, we can also see that if the session ID is changing, we will not be able to track session_onend. Once the session ID is stable, session_onend will appear.

Now let's talk about another condition. We should start from the experiment first. Based on the example above (including session_onstart,
Session_onend), add session. Abandon () under the session line of page_load ().
Run the command again, and then you will find something strange,
Session_onend is not executed, even though session_onstart has been executed once. (Here we need to write some log statements to observe) and
Session. Abandon () is written in the button. onclick event, and session_onend is executed immediately. What's the difference?

In this way, the second necessary condition is triggered. To make session_onend successfully run, at least one request must have been fully executed. In the first case above, if it is aborted in page_load, the first request is not executed, and session_onend cannot be stimulated.

Based on these two necessary conditions, we can finally obtain the sufficient conditions for executing session_onend:

◆ At least one request is successfully executed completely

◆ Store at least some data in the Session State. You can use the Session variable or Session_OnStart.

Finally, it is declared that Session_OnEnd is only supported in InProc mode, that is, it is only supported in Session Data in ASP. NET worker process.

The seven-point introduction to ASP. NET sessions can help you understand ASP. NET sessions?

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.