I have a web application and have used a lot of Session State in every webpage wherever we have used Devexpress GridView. Since GridView does partial postback, data gets lost in ViewState so we have to use Session to store data.
Although, we have disposed the Session on Master Page Init using below code:
protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<string> lstNecessarySessionKeys = new List<string> { "ConnectionString",
"LoggedInUser" };
List<string> lstSessionKeysToRemove = new List<string>();
foreach (object key in Session.Keys)
{
string keyName = key.ToString();
if (!lstNecessarySessionKeys.Contains(keyName))
{
lstSessionKeysToRemove.Add(keyName);
}
}
foreach (string keyName in lstSessionKeysToRemove)
{
Session.Remove(keyName);
}
}
}
But, i doubt if its really getting removed from memory of server. What will happen if there are 100's of user using the application simultaneously and if Session is not getting removed from the server, it might...crash!
When your ASP.NET process exceeds the memory limit configured in IIS, the process will be terminated and restarted. This is similar to an AppPool recycle. Then if you are using in-process Session, all the users will no longer have their session and will
have to login again. So technically the server won't crash.
But i can not use Session.Clear() to dispose the Session variables since it would dispose off all the session variables but i don't want to dispose 2 or more session variables.
Is there any harm on using Session.Remove(string)? The link you posted doesn't say much about use of Session.Remove(string).
AmitGovil
Member
58 Points
70 Posts
How to dispose Session State
Feb 01, 2013 09:56 AM|LINK
Hi
I have a web application and have used a lot of Session State in every webpage wherever we have used Devexpress GridView. Since GridView does partial postback, data gets lost in ViewState so we have to use Session to store data.
Although, we have disposed the Session on Master Page Init using below code:
protected void Page_Init(object sender, EventArgs e) { if (!IsPostBack) { List<string> lstNecessarySessionKeys = new List<string> { "ConnectionString", "LoggedInUser" }; List<string> lstSessionKeysToRemove = new List<string>(); foreach (object key in Session.Keys) { string keyName = key.ToString(); if (!lstNecessarySessionKeys.Contains(keyName)) { lstSessionKeysToRemove.Add(keyName); } } foreach (string keyName in lstSessionKeysToRemove) { Session.Remove(keyName); } } }But, i doubt if its really getting removed from memory of server. What will happen if there are 100's of user using the application simultaneously and if Session is not getting removed from the server, it might...crash!
Any help or suggestion!
DarrellNorto...
All-Star
87431 Points
9717 Posts
Moderator
MVP
Re: How to dispose Session State
Feb 01, 2013 10:09 AM|LINK
When your ASP.NET process exceeds the memory limit configured in IIS, the process will be terminated and restarted. This is similar to an AppPool recycle. Then if you are using in-process Session, all the users will no longer have their session and will have to login again. So technically the server won't crash.
The easier way to remove everything is to call Session.Clear().
More details on removing Session variables can be found in this blog post: http://abhijitjana.net/2011/06/04/asp-net-internals-clearing-asp-net-session-variables-a-in-depth-look/
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
AmitGovil
Member
58 Points
70 Posts
Re: How to dispose Session State
Feb 01, 2013 10:34 AM|LINK
Thanks for the reply!
But i can not use Session.Clear() to dispose the Session variables since it would dispose off all the session variables but i don't want to dispose 2 or more session variables.
Is there any harm on using Session.Remove(string)? The link you posted doesn't say much about use of Session.Remove(string).
jprochazka
Contributor
4992 Points
748 Posts
Re: How to dispose Session State
Feb 01, 2013 10:43 AM|LINK
You can remove each individually using Session.Remove("VariableName")
AmitGovil
Member
58 Points
70 Posts
Re: How to dispose Session State
Feb 04, 2013 08:13 AM|LINK
I'm already doing that!
jprochazka
Contributor
4992 Points
748 Posts
Re: How to dispose Session State
Feb 05, 2013 05:32 AM|LINK
Then they are being removed.