1. First, how to stimulate the Session_End () method
Because this method only supports sessions of the InProc (in-process) type, we configure Web. config as follows: <sessionState timeout = "1" mode = "InProc">
</SessionState>
Note: The basic unit of timeout is minute.
2. Why does Session. Abandon () still get the value from the Session?
Why cannot I obtain the HttpContext. Current object in Session_End?
For example:
Aspx part
<Form id = "form1" runat = "server">
<Div>
<Asp: Literal ID = "lblMsg" runat = "server"> </asp: Literal>
<Br/>
<Asp: Button ID = "btnTest" runat = "server" Text = "deregister Session" OnClick = "btnTest_Click"/>
<Asp: Button ID = "Button1" runat = "server" Text = "resend"/>
</Div>
</Form>
Aspx. cs part protected void Page_Load (object sender, EventArgs e)
{
If (! Page. IsPostBack)
{
This. Session ["state"] = 1;
}
This. lblMsg. Text = Convert. ToInt32 (Session ["state"]). ToString ();
}
Protected void btnTest_Click (object sender, EventArgs e)
{
This. Session. Abandon ();
// Reset lblMsg. Text
This. lblMsg. Text = Convert. ToInt32 (Session ["state"]). ToString ();
}
Global. ascx part void Session_End (object sender, EventArgs e)
{
Try
{
HttpContext. Current. Response. Write ("Call Session_End () method ");
}
Catch
{
Throw new Exception ("Capture exceptions ");
}
}
Run the test:
1) after the program is run, click the "deregister Session" button, even if you reset lblMsg. text, the output value is still 1, and it is not as expected, it should be that the Session is canceled, and then through Convert. after ToInt32 is converted to NULL, 0 is output.
2) Add a new BUTTON control to the ASPX page, run the program again, click "logout Session"-"Send back" in sequence, and the result is 0 after 2nd clicks. Through debugging, It is also found that after Session. Abandon () is called, The Session_End () method is indeed entered.
Note: The Session_End () method can be activated in step 1 and Step 2 through debugging.
Debugging analysis:
1) Prerequisites: click "cancel Session ".
Set a breakpoint for the code in btnTest_Click (). After debugging, you can find that the execution sequence is as follows:
No: From Session. Abandon ()-To Session_End ()-after execution, return to btnTest_Click () to continue executing other events.
Instead, execute all the events in btnTest_Click ()-and then run Session_End ()
2) Prerequisites: None
Set the breakpoint in Session_End () and start the page normally. Because I set the timeout of sessionState to 1 minute, I will do nothing at all. When one minute passes, when the program automatically enters Session_End (), it is clear to execute it here. As many cnblogs posts have mentioned, "Session_End () is an event handler function that is triggered inside the server, it is based on the Internal timer of a server, because there is no related HttpRequest object on the server when the event is triggered, so the concept of HttpContext does not exist. As for why the client needs to send a Request again to get 0, it is clear from the first step above.
Summary:
1) To activate Session_End (), you must configure Web. config correctly, as shown in <sessionState timeout = "1" mode = "InProc"/>
2) The HttpContext object cannot be obtained in Session_End.
3) after Session. Abandon () is executed, the client must have at least one request to correctly reflect the Session status.