Asp. Some understanding and questions of session in net

Source: Internet
Author: User
Tags session id net reference variable window client
Asp.net|session

1 for a variable of value type, a copy of the value type is saved in the session

session["__test0"] = 1;
int i = (int) session["__test0"]+1;
int j = (int) session["__test0"];
Results i=2,j=1

2 for reference class new variable, the session is saved in the reference

Cdacommon CDA = new Cdacommon ();
session["__test"] = CDA. GetDataSet ("SELECT top 1 * tb_customer");
DataSet ds = (DataSet) session["__test"];
DataSet ds2 = (DataSet) session["__test"];
Ds. Tables[0].    rows[0][0]= "9999"; Results ds. Tables[0]. rows[0][0]== "9999" DS2. Tables[0]. rows[0][0]== "9999";

3 The new browser window starts, starts a new session, triggers the global session_start call, the browser window opened from the first browser window does not start a new session. After the session expires, the execution of the page's submission also triggers the session_start, which is equal to the new one.

4 for Web Service, the invocation of each method initiates a session, and 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 that the Web service sets Cookiecontainer properties for the proxy class, as long as the Cookiecontainer property of multiple agents is the same value, the Web Service invocation in the same session. Can be implemented in single case mode.

5 as long as the page has a commit activity, all items of the session will be maintained, and the session will not expire in 20 minutes (default configuration) without any submission activity. Multiple data items stored in the session are invalidated as a whole.

6 If you save a serializable analogy such as DataView in session, you cannot use it in the mode of Save session with SQL Server. The way to see whether a class is serialized is to see whether the class is marked with [Serializable].

7 about the removal of Sesson, not too clear. If I save a larger dataset in the session, so that aspnet_wp.exe occupies a large amount of memory, if I quit the page that uses this dataset, I want to release it, I use Session.clear () or dataset.clear () can not make the footprint of memory down, even if the session over the deadline, memory has not come down, more confused, who can give me a detailed explanation.

When it comes to the session, many people may be dismissive. This dongdong, n years ago began to do, what good to speak of AH. However, in many places we will still find some problems, such as some people say, my session_start excited, how Session_End did not ah, I did some clean-up work in Session_End, this can not be completed, how to do?

Recently read some articles, combined with some of their own experience, I would like to discuss the argument with you.

In fact, a lot of this kind of problem is caused by a thing, it is the session ID. First of all, is not I an IE client up to visit 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, my browser is not closed, Web server will always think I am the same client, will not change the session ID to change. To verify this, let's do a simple experiment now. Create a simple asp.net web App with vs.net. Add a button to the Web Form1, and then enable trace on the page prefix of the pages. Next browse this page and click the button to submit the request. Thanks to this trace feature of ASP.net, we can see that the session ID is actually constantly changing. In other words, at this time on the server side, simply do not care about the existence of the client, each thought it came from a new client.

So what the hell is going on here? OK, let's add a sentence in the Page_Load, session["Variable1"]= "TestValue", and then do a test. Bingo, the session ID is now consistent. I think a lot of people may not have noticed this before. Here we can draw a conclusion: to create a continuous session, we need to use at least the session variable, in the jargon, is to write at least once to the session dictionary.

It is noteworthy, however, that this is a necessary condition, not a sufficient one.

Before we mention the next necessary condition, let's find out one thing, if we have global.asax in the middle of the program, there are Session_OnStart, Session_OnEnd, the experiment above will not be successful. The reason is that once the Session_OnStart handler is defined, the state of the session is always saved, and even if it is empty, the session ID will not change. Because session this thing still consumes resources, so in ASP.net web app if not necessary, you do not put Session_OnStart, Session_End write in Global.asax.

In the above experiment, we can also see that if the session ID is changing, we will not track the Session_OnEnd, once stabilized, Session_OnEnd appears.

Now, let's talk about another condition, or start with the experiment, and we'll add a sentence on the basis of just the example (including Session_OnStart, Session_OnEnd), under the line of Page_Load's session, Session.Abandon (). Again to run a, gee, this is something you will find a little strange place, Session_OnEnd not executed, although Session_OnStart performed over again. (Here we need to write some log statements to observe) and if we write Session.Abandon () in the Button.onclick event, Session_OnEnd immediately executes it. Strange, what difference does it have here?

In this way, the second requirement is raised, and for Session_OnEnd to execute successfully, at least one request has been fully executed. The first case above, in the Page_Load, the first request has not been completed, Session_OnEnd can not inspire.

By synthesizing these two necessary conditions, we can finally draw a sufficient condition for the Session_OnEnd to execute:

1 There is at least one request successfully executed in full
2 storing at least some data in the session state. Can be implemented by the session variable or by adding Session_OnStart.
Finally, it is stated that Session_OnEnd is only supported in InProc mode, that is, only when session data is in the 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.