ASP. NET Session 7: 00, session 7
We will encounter many problems when using ASP. NET sessions. Here we will talk about some common ASP. NET sessions:
For value-type variables, the Session stores copies of the value type.
123 |
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.
12345 |
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 ";
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 from 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.
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.
12 |
CWSSyscfg cwsCfg = new CWSSyscfg(); cwsCfg.CookieContainer = new System.Net.CookieContainer(); |
CWSSyscfg is a Web Service class. The CookieContainer attribute is set for the proxy class by the Web Service. 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.
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.
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 Sessuon 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 questions. For example, some people say that my session_start is motivated. Why does session_end fail? I have done some Aftercare Work in session_end, this cannot be done. What should I do?
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. NET to create a simple ASP. NET Web App. add a Button to Web Form1 and enable trace on the Page Prefix. next, go to this page and click the button 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 for ASP. NET Web apps, do 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. In this case, you may find that 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?