ASP. NET Session 7

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, a new Session is started to trigger the Global Session_Start call. The new Session is not started in the browser window opened in the first browser window. 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. The Web Service sets the CookieContainer attribute for the proxy class. As long as the CookieContainer attribute of multiple proxies is the same value, the Web 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 Sesson's cleanup.

If I keep a relatively large datasetin session, the memory occupied by aspnet_wp.exe will be very large. If I quit the page using this DataSet, I want to release this Session, and I will use the Session. clear () or DataSet. clear () cannot reduce memory usage. Even if the Session expires, the memory has not been downgraded. 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, in many places, we still find some problems. For example, some people say that my session_start is inspired. Why does session_end not exist? I have done some Aftercare Work in session_end, this cannot be done. How can this problem be solved?

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 of all, is it because I access a page from an IE client and 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 the web server will always think that I am the same client and will 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 enable trace on the page prefix. Then, browse the page and click the button continuously to submit the 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. Every time we think it is from a new client.

So what is the problem? OK. Let's add session ["variable1"] = "testvalue" in page_load, and then perform a test. Bingo. Now the session 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. In the line, we need to write at least once into the session dictionary.

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 clarify one thing first. If we have global. asax in the middle of the program, which contains session_onstart and session_onend, the above experiment won't succeed. The reason is that after the session_onstart processing function is defined, the session state will always be saved, even if it is empty, the session ID will not change. Sessions consume resources, so if there is no need to write session_onstart and session_end in asp.net web apps, you should 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. Let's start with the experiment. Based on the example above (including session_onstart, session_onend), add a sentence below the session line of page_load, session. abandon (). run another one, Skip. This is something you may find strange. session_onend is not executed, 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 will be 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? (Source: jezilan)

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.