We will encounter a lot of problems in the use of ASP.NET Session, so here we talk about the understanding of some common ASP.NET Sessions that often appear:

1)One of the seven points of ASP.NET Session:For variables 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)Two of the seven points of ASP.NET Session:For a new variable of the reference class, the session holds a reference.

      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 ” ;

  The result ds.Tables[0].Rows[0][0]==”9999″ ds2.Tables[0].Rows[0][0]==”9999″;

3)Three of the seven points of ASP.NET Session:Session period

  After the new browser window is launched, a new session is started, triggering the Global Session_Start call, and the browser window opened from the first browser window does not start a new session. After the session expires, the execution of the execution page will also trigger Session_Start, which is equal to a new session.

4)Four of the seven points of ASP.NET Session:Call Session

  For Web Service, each method call starts a Session. You can use the following methods 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 property to the proxy class. As long as the CookieContainer properties of multiple agents are the same value, the calls to these Web Services are in the same Session. Can be implemented in singleton mode.

5)Five of the seven points of ASP.NET Session:Session data validity period

  As long as the page has a commit activity, all items in the Session will be maintained, and the Session will be invalid when there is no commit activity within 20 minutes (the default configuration). Multiple data items stored in the Session are generally invalid.

6)Six of the seven points of ASP.NET Session:Session save

  If you save a non-serialized class such as DataView 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].

7)Seven of the seven points of ASP.NET Session:About the removal of Sessuon

  If I save a large DataSet in the Session, the memory occupied by aspnet_wp.exe will be very large. 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 has expired, the memory has not come down, more confused, who can give me a detailed explanation.

  To talk about the session, many people may not care. This Dongdong, I started doing it n years ago, and I have a good talk. However, in many places we will still find some problems, for example, some people say that my session_start is motivated, how is session_end not, I have done some after-work in session_end, this can’t be done, what should I do?

  I have read some articles recently and combined with some of my own experiences, I would like to discuss with you some of them.

  In fact, many of these problems are caused by one thing, it is the Session ID. First of all, is it possible for me to get an IE client and access a page. As long as I don’t close the browser, is the Session ID the same? Many people will think, it should be the same, my browser is not closed, Web Server will always think that I am the same client, will not change the Session ID. To verify this, let’s do a simple experiment now. Create a simple ASP.NET Web App with VS.NET. Add a Button to Web Form1, and then enable trace on the page’s Page Prefix. Next, browse the page and click the button to submit the Request. Thanks to the trace function of ASP.NET, we can see that the Session ID is actually changing. In other words, at this time on the server side, there is no concern about the existence of this client, every time I think it is from a new client.

  So what is going on here? OK, let’s add a sentence to Page_Load, Session[“variable1″]=”testvalue”; then do another test. Bingo, the Session ID is now consistent. I think many 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 jargon, to write at least once to the Session Dictionary.

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

  Before we mention the next necessary condition, let’s first clarify one thing. If we have Global.asax in the middle of the program, there are Session_OnStart, Session_OnEnd, the above experiment will not succeed. 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 the Session ID will not change. Because Session is still consuming resources, you don’t want to write Session_OnStart, 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 will not track the Session_OnEnd. Once stabilized, the Session_OnEnd will appear.

  Now, let’s talk about another condition, or start with the experiment. On the basis of the previous example (including Session_OnStart, Session_OnEnd), add a sentence below the Session line of Page_Load, Session.Abandon(). Run one, hey, then you will find something strange, Session_OnEnd is not executed, although 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 execute immediately. Strange, what is the difference here?

  In this way, the second requirement is triggered. For Session_OnEnd to execute successfully, at least one Request must have been executed completely. In the first case above, if the page_Load is aborted, the first Request is not executed, and the Session_OnEnd cannot be fired.

  Combining these two necessary conditions, we can finally get the full conditions for the Session_OnEnd to execute:

  ◆ At least one Request is successfully executed completely

  ◆ Store at least some data in the Session State. This can be done with the Session variable or by adding Session_OnStart.

  Finally, the Session_OnEnd is only supported in the InProc mode, that is, it is only supported when the Session Data is in the ASP.NET worker process.

  The seven points of ASP.NET Session are mentioned here. Is the understanding of ASP.NET Session helpful?