ASP. NET session usage report (posting)

Source: Internet
Author: User
When talking about session, many people may be dismissive. Well, it started N years ago.
Let's talk about it. However, we still find some problems in many places, for example, some people say that my
Session_start is triggered. Why isn't session_end? I have done some Aftercare Work in session_end,
This cannot be done. What should I do?

Recently I read someArticleIn combination with some of your own experiences, I 'd like to discuss some of these statements with you.

In fact, many of these problems are caused by one thing, which is the session ID. First, is it me?
When I access a page from an IE client, the session ID is the same if I don't close my browser?
A lot of people will think, it should be the same. My browser is not closed, and web server will always think that I am the same
The client does not change the session ID. 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 Web form1, and then
Enable trace on page prefix. Visit this page and click the button to submit
Request. Thanks to the Trace feature of Asp.net, we can see that the session ID is constantly changing.
. That is to say, at this time, on the server side, we don't care about the existence of this client.
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 the session ID is guaranteed
Consistent. I think many people may not have noticed this before. Here we can draw a conclusion: Yes
To create a continuous session, we need to use at least the session variable.
Write less to session dictionary once.

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

Before we mention the next necessary condition, let's clarify one thing first, if weProgramIntermediate
Global. asax, which contains session_onstart and session_onend, won't be successful in the above experiment. Original
Because once the session_onstart processing function is defined, the session State will always be saved, even if
It is empty, so the session ID will not change. Because session still consumes resources.
So if there is no need in Asp.net web app, you should not set session_onstart,
Session_end is written 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 it is stabilized, session_onend will appear.

Now let's talk about another condition. We should start from the experiment first.
Including session_onstart and session_onend). Add the following sentence to the Session line of page_load,
Session. Abandon (). Run it again, Skip. This is something you will find strange. session_onend is not
Run, even though session_onstart has been executed again. (Here we need to write some log statements to observe
And if we write session. Abandon () in the button. onclick event,
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
The request has been fully executed. In the first case above, if it is aborted in page_load, the first
If the request is not completed, session_onend cannot be triggered.

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

? 1) At least one request is successfully executed completely.

2) 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, only in session
Data is supported in Asp.net worker process.
 

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.