Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the seven understandings of ASP.NET Session?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces to you what are the seven points of understanding of ASP.NET Session, the content is very detailed, interested friends can refer to, hope to be helpful to you.

We will encounter a lot of problems in the use of ASP.NET Session, so let's talk about some common understanding of ASP.NET Session:

One of the seven understandings of ASP.NET Session,

For variables of value types, a copy of the value type is saved in Session

Session ["_ test0"] = 1; int I = (int) Session ["_ test0"] + 1; int j = (int) Session ["_ test0"]

As a result, iTunes 2 and jungle 1

The second of the seven understandings of ASP.NET Session.

For new variables of the reference class, references are saved in 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"

Results ds.Tables [0] .Rows [0] [0] = "9999" ds2.Tables [0] .Rows [0] [0] = "9999"

The third of the seven understandings of ASP.NET Session.

Session cycle

After a new browser window is launched, a new Session is started, which triggers a call to the Session_Start of Global. A browser window that opens from * browser windows does not launch a new Session. After the Session expires, the submission of the execution page also triggers the Session_Start, which is equivalent to a new Session.

The fourth of the seven understandings of ASP.NET Session.

Call Session

For Web Service, each method call starts 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, and Web Service sets the CookieContainer property to the proxy class. As long as the CookieContainer attribute of multiple proxies is the same value, the call to these Web Service is in the same Session. It can be implemented in singleton mode.

The fifth of the seven understandings of ASP.NET Session.

Validity period of Session data

As long as there is a submission activity on the page, all items in the Session will be maintained, and the Session will expire if there is no submission activity within 20 minutes (default configuration). Multiple data items stored in Session are invalid as a whole.

The sixth of the seven understandings of ASP.NET Session.

Preservation of Session

If a non-serialized class such as DataView is saved in session, it cannot be used in the mode of saving Session with SQLServer. The way to see if a class is serialized is to see if it is marked with [Serializable].

Seven points of understanding of ASP.NET Session.

About the removal of Sesson.

If I save a larger DataSet in Session, so aspnet_wp.exe occupies a lot of memory, if I quit the page using this DataSet, I want to release the Session, I use Session.Clear () or DataSet.Clear () can not make the memory down, even if the Session passed the deadline, memory did not come down, more confused, who can give me a detailed explanation.

When it comes to session, many people may disdain it. This thing began to be made n years ago. There is nothing to talk about. However, in many places we will still find some problems, such as some people said, my session_start excited, how not session_end ah, I did some follow-up work in session_end, this can not be completed, how to do ah?

Recently, I have read some articles and combined with my own experience, I would like to discuss the views with you.

In fact, many of these problems are caused by one thing, that is, session ID. First of all, am I an IE client to visit a page, as long as I do not close the browser, session ID is the same? Many people will think, it should be the same, my browser is open, web server will always think that I am the same client, will not change the session ID back and forth. 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 page. Next, browse the page and keep click button to submit the request. Thanks to the trace feature of asp.net, we can see that session ID is actually constantly changing. In other words, at this time, on the server side, you don't care about the existence of this client at all, and every time you feel that it comes from a new client.

So what's going on here? OK, let's add a sentence to page_load, session ["variable1"] = "testvalue"; then do the test again. Bingo, now session ID is consistent. I think a lot of people may not have noticed this before. Here we can draw a conclusion: to build a persistent session, we need to use the session variable at least, which, in jargon, is to write to the session dictionary at least once.

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

Before we mention the next necessary condition, let's be clear about one thing: if we have global.asax in the middle of the program, and there are session_onstart and session_onend in it, the above experiment will not be successful. The reason is that once the session_onstart handler is defined, the state of the session will always be saved, even if it is empty, so that the session ID will not change. Because session still consumes resources, you should not write session_onstart and session_end in global.asax if it is not necessary in asp.net web app.

In the above experiment, we can also see that if the session ID is changing, we can't track the session_onend, and once it stabilizes, the session_onend appears.

Now, let's talk about another condition, let's start with the experiment. On the basis of the examples just now (including session_onstart, session_onend), we add a sentence, session.abandon (), at the bottom of the session line of page_load. Let's run it again. Gee, this is what you'll find a little strange about. Session_onend doesn't execute, 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 executes immediately. Strange, what's the difference here?

In this way, the second necessary condition arises that at least one request must have been fully executed for session_onend to be executed successfully. In the case of * above, if the request is terminated in page_load, the session_onend will not be activated if the execution of * is not finished.

Combining these two necessary conditions, we can finally arrive at the sufficient conditions for session_onend to execute:

◆ has at least one request successfully executed in its entirety

◆ stores at least some data in session state. This can be done through the session variable or by adding session_onstart.

To be clear, session_onend is only supported in InProc mode, that is, only when session data is in asp.net worker process.

This is the end of the seven-point understanding of ASP.NET Session. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report