locked
.Net Session State using the State Server RRS feed

  • Question

  • User950810374 posted

    Hi,

    I am using State Server to store my session state for our application.

    Is there a way I can check the size of the session data stored ?

    Friday, February 5, 2016 5:04 AM

All replies

  • User36583972 posted

    Hi Mahajan_Pranshu,

    From your description, I try to give you the following method for you to check the size of the session data stored.

                long totalSessionBytes = 0;
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter b =
                    new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                System.IO.MemoryStream m;
                foreach (string key in Session)
                {
                    var obj = Session[key];
                    m = new System.IO.MemoryStream();
                    b.Serialize(m, obj);
                    totalSessionBytes += m.Length;
                }
                Response.Write("Session Trace Info:"+String.Format("Total Size of Session Data: {0:n} kb", totalSessionBytes / 1024));

    The information about Exploring Session in ASP.NET for your reference:

    http://www.codeproject.com/Articles/32545/Exploring-Session-in-ASP-Net

    Best Regards,

    Yohann Lu

    Friday, February 5, 2016 7:04 AM
  • User950810374 posted

    Thank you for your response, this what i get when i try and run the above script :

    Attempting to perform the InitializeDefaultDrives operation on the 'FileSystem' provider failed.


    PS C:\Windows\system32> long totalSessionBytes = 0;
    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter b =
    new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
    System.IO.MemoryStream m;
    foreach (string key in Session)
    {
    var obj = Session[key];
    m = new System.IO.MemoryStream();
    b.Serialize(m, obj);
    totalSessionBytes += m.Length;
    }
    Response.Write("Session Trace Info:"+String.Format("Total Size of Session Data: {0:n} kb", totalSessionBytes / 1024));
    At line:3 char:84
    + ... inaryFormatter();
    + ~
    An expression was expected after '('.
    At line:5 char:22
    + foreach (string key in Session)
    + ~
    Missing variable name after foreach.
    At line:5 char:43
    + foreach (string key in Session)
    + ~
    Unexpected token ')' in expression or statement.
    At line:7 char:17
    + var obj = Session[key];
    + ~~~
    The 'var' keyword is not supported in this version of the language.
    At line:8 char:48
    + m = new System.IO.MemoryStream();
    + ~
    An expression was expected after '('.
    At line:9 char:30
    + b.Serialize(m, obj);
    + ~
    Missing argument in parameter list.
    At line:12 char:50
    + Response.Write("Session Trace Info:"+String.Format("Total Size of Se ...
    + ~
    You must provide a value expression following the '+' operator.
    At line:12 char:50
    + Response.Write("Session Trace Info:"+String.Format("Total Size of Se ...
    + ~~~~~~~~~~~~~
    Unexpected token 'String.Format' in expression or statement.
    At line:12 char:50
    + Response.Write("Session Trace Info:"+String.Format("Total Size of Se ...
    + ~
    Missing closing ')' in expression.
    At line:12 char:103
    + ... ata: {0:n} kb", totalSessionBytes / 1024));
    + ~
    Missing expression after ','.
    Not all parse errors were reported. Correct the reported errors and try again.
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedExpression

    PS C:\Windows\system32>

    Friday, February 5, 2016 7:13 AM
  • User36583972 posted

    Hi Hi Mahajan_Pranshu,

    I don't know if you are debugging the way the above code is correct. I suggest you try it in the following way.

    1: Create a new ASP.NET project (WebForm)

    2: Create a new WebForm page

    3: Define a method in the page, the add the reference code for debugging.

    Finally, you can share us some information to help you analyze.

    Best Regards,

    Yohann

    Saturday, February 20, 2016 5:24 PM
  • User753101303 posted

    Hi,

    This is not a PowerShell script but C# code that should be placed inside the app (and it shows the size only for the current session).  As it seems you are rather on the admin side, you could likely just start by looking at the memory consumption for the aspnet_state.exe process.

    Monday, March 21, 2016 5:22 PM
  • User765422875 posted

    A bit of context in case you are thinking of these things.

    Out of process state management using the session state server is limited to the amount of memory available to the state service. So it depends on whether you are using a 32-bit machine or a 64-bit one. Not sure if its relevant to you at this time, but from what I'm seeing the best performance for managing state is with SQL Server - specifically with memory optimized tables.

    Check out the following post: https://blogs.msdn.microsoft.com/kenkilty/2014/07/03/asp-net-session-state-using-sql-server-in-memory/

    And here is a piece of code you can use to check your current session size.

    private void CheckSessionSize()
    {
        long totalSessionBytes = 0;
        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter b = 
            new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        System.IO.MemoryStream m;
        foreach (var key in Session)
        {
            var obj = Session[key];
            ms = new System.IO.MemoryStream();
            b.Serialize(ms, obj);
            totalSessionBytes += ms.Length;
        }
    
        Page.Trace.Write(String.Format("Total Size of Session Data: {0:n} kb", 
           totalSessionBytes / 1024));
    }
    Tuesday, March 22, 2016 12:19 PM