locked
How to dispose Session State RRS feed

  • Question

  • User1669525285 posted

    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!

    Friday, February 1, 2013 5:56 AM

Answers

All replies

  • User-837620913 posted

    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/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 1, 2013 6:09 AM
  • User1669525285 posted

    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).

    Friday, February 1, 2013 6:34 AM
  • User-1404016747 posted

    You can remove each individually using Session.Remove("VariableName")

    Friday, February 1, 2013 6:43 AM
  • User1669525285 posted

    You can remove each individually using Session.Remove("VariableName")
    

    I'm already doing that!

    Monday, February 4, 2013 4:13 AM
  • User-1404016747 posted

    jprochazka

    You can remove each individually using Session.Remove("VariableName")
    

    I'm already doing that!

    Then they are being removed.

    Tuesday, February 5, 2013 1:32 AM