A seven point summary of ASP. NET Session

  • 2020-05-12 02:31:30
  • OfStack

ASP.NET Session

For value type variables,Session holds a copy of the value type

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

ASP.NET Session's seven-point cognition 2

For new variables in the reference class, the reference is held in Session

CDACommon cda = new CDACommon(); S 36en [" s 41en "] = 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";

ASP.NET Session's 7 points of knowledge 3

Session cycle

After the new browser window starts, a new Session is started, triggering the call of Session_Start of Global. The browser window opened from the first browser window does not start the new Session. When Session expires, the submission of the execution page will also trigger Session_Start, which is equivalent to a new Session.

ASP.NET Session's 7 points of knowledge 4

Call Session

For Web Service, each method call initiates one 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 one Web Service class, and Web Service sets the CookieContainer property to the proxy class. As long as the CookieContainer property of multiple proxies is the same value, the calls to these Web Service are in the same Session. You can do this with a singleton pattern.

ASP.NET Session
Date of validity of Session data
As long as there is a submit activity on the page, all items in Session will remain, and Session will fail if there is no commit activity on the page for 20 minutes (the default configuration). Multiple data items stored in Session are globally invalid.

ASP.NET Session
The preservation of Session

If you save a non-serialized class such as DataView in session, you cannot use it in SQLServer mode to save Session. The way to see if a class is serialized is to see if the class is marked with [Serializable].

ASP.NET Session

Clearance of Sesson.

If I save in Session a larger DataSet, such aspnet_wp. exe occupy memory will be very big, if I quit using the DataSet page, I want to release the Session, I use Session Clear () or DataSet. Clear () can make the memory footprint down, even Session over the time limit, the memory also does not have to come down, more confusion, who can give me a more detailed explanation of 1.

When it comes to session, a lot of people may not care about it. This thing, n began to do before, what good to say ah. However, we will still find some problems in many places. For example, some people say, "my session_start has been activated. Why not session_end has not been activated?

Recently read some articles, combined with their own 1 some experience, want to discuss 1 with you 1 under the statement.

In fact, many of these problems are caused by one thing, session ID. First of all, is IE client 1 up, access 1 page, as long as I don't turn the browser, session ID is 1? A lot of people will think, it should be 1, my browser is not off, web server will always think that I am the same one client, session ID will not change. To verify this, let's now do a simple experiment. Create a simple asp. net web app. Add button to web form1 and then enable trace on page prefix. Thanks to the asp.net trace feature, we can see that session ID is constantly changing. In other words, at this point on the server side, you don't even care about the existence of client, every time you think it's coming from a new client.

So what's going on here? OK, let's add a sentence to page_load, session["variable1"]="testvalue"; Then do another test. Bingo, so now session ID is 1 to 1. I think a lot of people may not have noticed this before. Here's the takeaway: to create a sustainable session, we need to use at least 1 session variable, which in the jargon means at least once written into session dictionary.

However, it is important to note that this is only a necessary condition, not a sufficient one.

Before mentioning the next necessary condition, let's make one thing clear: if we have global.asax in the middle of the program, session_onstart, session_onend, the above experiment will not succeed. The reason is that after 1 defines the session_onstart handler, state of session will always be saved, even if it is empty, so session ID will not be changed. Since session still consumes resources, you should not write session_onstart, session_end in asp.net web app if it is not necessary.

In the above experiment, we can also see that if session ID is changing, we cannot track session_onend. Once the session_onend is stable, session_onend appears.

Now, let's talk about another condition, let's start with the experiment, let's build on the example (session_onstart, session_onend), page_load session, session.abandon (), and let's run 1, well, this is one of the odd things you'll find,session_onend doesn't execute, even though session_onstart has been executed once. Also, if we write session.abandon () in the button.onclick event, session_onend will execute immediately. Strange, what difference does it make?

Thus, the second requirement is raised that for session_onend to execute successfully, at least one request must have been fully executed. In the first case above, if the page_load is aborted, the first request is not executed, and session_onend cannot be fired.

By combining the two necessary conditions, we finally arrive at a sufficient condition for session_onend to execute:

◆ at least 1 request has been successfully and completely executed

◆ store at least 1 data in session state. You can do this by the session variable or by adding session_onstart.

Last but not least, session_onend is only supported in InProc mode, that is, only when session data is supported in asp.net worker process.

So much for ASP NET Session's seven-point understanding. Does it help to understand ASP NET Session?

Related articles: