ASP.NET Session Monitor 1.0 Released to MSDN Code Gallery

A couple of years ago I was dealing with an ASP.NET application with session state issues.  This happens, if you can believe it.  I was flummoxed by the problem and the routine checks and configuration options didn’t seem to fix the problem (although there was one little thing that was causing me problems and I didn’t catch it before I went down the path I am about to write about).  I decided I needed to find a way to peer into ASP.NET session state.  Given that the web application was storing state in Microsoft SQL Server, the challenge was to figure out how to make sense of the state information stored in the database.  For background information, check out this tag from my old blog.

If you have read some of those older posts, you might note that I wrote a session monitoring application a while back.  That code is somewhere in the mists of time and I wouldn’t use it as it was anyway because it had some issues, so I decided to take another swag at it.  Also, Philippe sent me an email asking for the tool, so I thought I would oblige and deliver it to everyone.

Here it is on MSDN Code Gallery.

ASP.NET Session Monitor 1.0

I had to read my own blog posts again and figure out how this works and it is a little rough, so consider this a starter project for somebody with more time who might improve it.  The basic requirements are as follows:

  1. Create a class that monitors ASP.NET session data (if Microsoft SQL Server is backing session; otherwise, you are on your own!)
    1. Detect new sessions and provide basic information about the session
    2. Detect the removal of a session
    3. Detect new session items and provide basic information about the value (primitives and strings are directly supported)
    4. Detect session item value changes and maintain a history of the values as they change
    5. Detect session item removal
  2. Create a simple user interface to display session information
    1. Enable impersonation so session state stored in a database residing outside the current authenticating domain can be monitored
    2. Display sessions, session values, and session value history in a comprehensible manner
    3. Support direct inspection of session values of primitive types and strings
    4. Provide a property browser for non-primitive\string types (this is an area for improvement if somebody wants to build an extension that can unpack non-primitive types, but I warn you that because the session monitor does not necessarily run on the same machine where the web application is running, you might have a problem with type resolution!)

Session Monitor User Interface

image

The key feature of the user interface shown above are:

  1. Collected credentials and use Win32 impersonation if specified by the user (use default credentials by checking the “Use Current” checkbox
  2. Provide a drop-down of all available Microsoft SQL Server instances
  3. Once an instance is selected, provide a dropdown of databases (in my case, the state database is the SharePoint shared services DB for my Project Server implementation)
  4. Allow for the user to specify a polling interval.  The default is zero, so think of this as a debugging aide and not something you would point to a production server
  5. Click the image button to start monitoring and image  to stop monitoring

I will leave it to you to peruse the source code and read this deep dive to understand how this works.  

Simple Demonstration

I created a very simple web application to demonstrate the session monitor:

image

Let’s start with a screenshot of the session monitor as it monitors, with no sessions currently in play:

image

In the test application, I add primitive (which is also the name of the value) value “14” and here is the result:

image

In the test application, I add a value to the list and here is the result:

image

In the test application, I update the primitive value to 23 and here is the result:

image

You can see that the session monitor picked up the new value and displayed it below the original value.

For non-primitive values or strings, you can click the View link and see basic properties:

image

That’s it folks!  Hope this is useful to somebody :-)

Colby