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:
- Create a class that monitors ASP.NET session data (if Microsoft SQL Server is backing session; otherwise, you are on your own!)
- Detect new sessions and provide basic information about the session
- Detect the removal of a session
- Detect new session items and provide basic information about the value (primitives and strings are directly supported)
- Detect session item value changes and maintain a history of the values as they change
- Detect session item removal
- Create a simple user interface to display session information
- Enable impersonation so session state stored in a database residing outside the current authenticating domain can be monitored
- Display sessions, session values, and session value history in a comprehensible manner
- Support direct inspection of session values of primitive types and strings
- 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
The key feature of the user interface shown above are:
- Collected credentials and use Win32 impersonation if specified by the user (use default credentials by checking the “Use Current” checkbox
- Provide a drop-down of all available Microsoft SQL Server instances
- 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)
- 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
- Click the button to start monitoring and to stop monitoring
I will leave it to you to peruse the source code and read this deep dive to understand how this works.
I created a very simple web application to demonstrate the session monitor:
Let’s start with a screenshot of the session monitor as it monitors, with no sessions currently in play:
In the test application, I add primitive (which is also the name of the value) value “14” and here is the result:
In the test application, I add a value to the list and here is the result:
In the test application, I update the primitive value to 23 and here is the result:
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:
That’s it folks! Hope this is useful to somebody