Question #1
I want to know when am I supposed to use:
Session.Abandon() // When I use this during tracing and after calling it- I find the session still has a value.
And when am I supposed to use :
When should I use each specific method?
I check if session is not equal null in Page Load. If session is equal to null, I wanna to clear session and redirect to the login page?
Should I use something like this:
private void initSession()
{
Session.Clear();
Session.Abandon();
Response.Redirect("LoginPage.aspx");
}
This question is related to
c#
.net
asp.net
session
page-lifecycle
The other big difference is Abandon does not remove items immediately, but when it does then cleanup it does a loop over session items to check for STA COM objects it needs to handle specially. And this can be a problem.
Under high load it's possible for two (or more) requests to make it to the server for the same session (that is two requests with the same session cookie). Their execution will be serialized, but since Abandon doesn't clear out the items synchronously but rather sets a flag it's possible for both requests to run, and both requests to schedule a work item to clear out session "later". Both these work items can then run at the same time, and both are checking the session objects, and both are clearing out the array of objects, and what happens when you have two things iterating over a list and changing it?? Boom! And since this happens in a queueuserworkitem callback and is NOT done in a try/catch (thanks MS), it will bring down your entire app domain. Been there.
Found this article on net, very relevant to this topic. So posting here.
Source: Stackoverflow.com