Click here to Skip to main content
11,578,954 members (63,629 online)
Click here to Skip to main content

Exploring ASP.NET Session State and Cache data

, 10 Jan 2007 CPOL 284K 5.5K 131
Rate this:
Please Sign up or sign in to vote.
An article showing how to create quick, "easy to use" views of session state and cache.

Introduction

On all the previous ASP and ASP.NET web sites I have worked on I have thought how useful it would be to have some quick and "easy to use" view of Session State and the Cache without having to sit with the debugger and manually step through each one.

As soon as you get a few developers working on a site you face the possibility of session and/or cache abuse. By this I do not mean to suggest that anything malicious might be going on, simply that there is the potential for sloppy use of these facilities. For example, putting information into session state that really shouldn't be in there or multiple developers putting identical data into session state under separate keys.

Under these circumstances it would be useful to have one or more utilities to allow you to view the contents of session state and the cache.

In this article I will show how to create these quick, "easy to use" views of session state and cache. Along the way we will touch on the subjects of HttpHandlers, custom configuration sections and binary serialization.

The full source code and a test web site is available to download.

Extending a Good Idea

Both our session and the cache views are implemented in a similar way to the standard trace tool that comes with ASP.NET, trace.axd.

The ASP.NET trace functionality is available to use in you ASP.NET code through the TraceContext class and the Page class’ Trace property. Information output to trace and a lot of other useful information such as an enumeration of session variables is rendered by the TraceHandler class that deals with the processing of the virtual page Trace.axd. The MSDN Online documentation on trace is here ASP.NET Trace.

Our session and cache views are custom HttpHandlers that are configured to process requests for the virtual pages of SessionView.axd and CacheView.axd. These names are determined using local web.config setting and the choice of them is up to the developer. I have given my "pages" an axd extension simply for consistency with the already well-known trace.axd. The article Extending ASP.NET with HttpHandlers and HttpModules from http://www.devx.com/ does a great job of explaining how to create a custom HttpHandler so I will not be covering that here.

The configuration xml to add the HttpHandlers to the ASP.NET pipeline is:

<httpHandlers>
<add verb="*" 
path="SessionView.axd"
type="Charteris.Web.HttpHandlers.SessionViewHandler,Charteris.Web.HttpHandlers" />
<add verb="*" 
path="CacheView.axd" 
type="Charteris.Web.HttpHandlers.CacheViewHandler,Charteris.Web.HttpHandlers" />
</httpHandlers>
            

If the HttpHandler's assembly was located in the Global Assembly Cache (GAC) you would need to add further location information with the type attribute changing from

type="Charteris.Web.HttpHandlers.SessionViewHandler,
      Charteris.Web.HttpHandlers"
        

to

type="Charteris.Web.HttpHandlers.SessionViewHandler,
      Charteris.Web.HttpHandlers, Version=2.0.2000.0, 
      Culture=neutral, 
      PublicKeyToken=4edafd72b7cb8805"
        

This extra GAC information is available from Windows Explorer when you browse to the %WindowsDir%\Assembly directory.

For security reasons I have implemented a check to ensure only requests from the local box are processed. These are really only dev tools so this restriction is not really an issue. The RequestIsLocal code is in the Common.cs class and simply checks if the request’s source IP address is the local loopback address of 127.0.0.1 or if it is the same as the server’s IP address.

///<summary>
/// Check if current request originated locally
///</summary>
///<param name="request">The current HttpRequest</param>
///<returns>True if the request originated locally</returns>
internal static bool RequestIsLocal(HttpRequest request){ 
    if(request.UserHostAddress == "127.0.0.1" || 
        request.UserHostAddress == request.ServerVariables["LOCAL_ADDR"]){ 
            return true; 
    } 
    else{ 
        return false; 
    } 
} 

The views both have a similar configuration style with the ability to disable the view and to disable the link, which allows viewing of the actual data stored within the session state or cache item. This link can be seen in the SessionView.axd screenshot in the following section.

These configuration options are implemented using a custom config handler. Custom config handlers inherit from IConfigurationSectionHandler and are used to process the config section that you set up in web.config. In the following piece of XML I have defined the config section and what handler will process it. Again the type attribute would need extra information if the config handler's assembly was in the GAC. The MSDN online docs for creating and processing custom config sections are very good and can be found here Creating New Configuration Sections.

As you may be aware Microsoft occasionally release best practice .NET code known as Application Blocks. There is a new block due for release this summer that deals with custom configuration, in .NET, using your own custom config files. If there is enough interest I will do an update to this article that uses the new Microsoft application block to handle the view's configuration needs.

SessionView.axd

The session state view enumerates all the items in Session State.

For each item it will render a row containing the item’s key in the session state collection and its type name.

If the Session State mode being used means that the items are serialized into the session state then it will also display an approximation of the storage space the object is taking up. When Session State is running InProc (In Process) the actual objects that form the content are not stored in the collection, only reference to the objects. The figures for the size taken up by these objects "in" session state would be misleading under these circumstances. Therefore there is no "Object Size" column when running in this mode. When Session State is running out of process, either using a StateServer machine or a SQL Server database, the objects are serialized into Session State.

The value that is displayed in the "Object Size" column is the byte size of a stream that is used as the destination of a binary serialization. This code to perform the serialization for most objects is shown below:

MemoryStream m;
m = Common.BinarySerialize(sessionItem);

//Size value
double size = Convert.ToDouble(m.Length) / Convert.ToDouble(1000) + " KB"; 

/// <summary>
/// Serialize object into a memory stream using Binary Serialization
/// </summary>
/// <param name="objectToSerialize">object to serialize</param>
/// <returns></returns>
internal static MemoryStream BinarySerialize(object objectToSerialize) {
    MemoryStream m = new MemoryStream();
    BinaryFormatter b = new BinaryFormatter();

    b.Serialize(m, objectToSerialize);

    return m;
}

Originally I was the above technique, for all types stored in Session and Cache, to get my calculations for approximate "Object Size". However after some discussion with one of the Microsft devs in the US I now know that ASP.NET uses an internal, optimized, serialization technique for some of the standard types. This technique is internal to the System.Web assmebly and is subject to change in future versions. I have imitated their approach and have listed below all the types that are currebtly being serialized in this fashion. This set of types is correct for the 1.1 version of the framework. Less types were serialised in this way in version 1.0.

  • String
  • Int16
  • Int32
  • Int64
  • Boolean
  • DateTime
  • Decimal
  • Byte
  • Char
  • Double
  • SByte
  • UInt16
  • UInt32
  • UInt64
  • Timespan
  • Guid
  • IntPtr
  • UIntPtr
  • Object

If the item’s type can be understood by the XmlSerializer class then the “View Data” cell will display a link to allow viewing of the contents of the item. This facility can be disabled by setting the config attribute showViewDataLink to false. I use the XmlSeriliazer to get a renderable view of the data. This means that there are extraneous XML tags at the top and bottom of the data when viewed.

CacheView.axd

In a similar fashion to the session state view, this enumerates the contents of the cache.

Again for every cache item, it will render a row containing the item’s key in the cache collection and its type name. As the cache is always run in process, the items are object references and therefore there is no “Object Size” column for the cache view.

Again like the session view, if it is not disabled by the config setting showViewDataLink and the item’s type is understood by the XmlSerializer, the “View Data” cell will display a link to allow viewing of the contents of the item.

Points of Interest

One gotcha that I found with developing an HttpHandler that wants to use session state was that by default you can’t. If you want your HttpHandler to have access to session state you need to implement the IRequiresSessionState interface.

History 

OK, After speaking to one of the Microsoft guys in the US there is a serious flaw in my thinking regarding calculating an approximate size for items in session state. ASP.NET "uses an optimized internal formatter for basic types such as int, string, bool, etc". Therefore me basing my calculation on the BinaryFormatter is wrong. I have updated the article saying that my use of the BinaryFormatter gives only a very rough approximation.

  • 31 May 2003 - updated downloads
  • 6 June 2003 - Amended Text

The article now takes into consideration that ASP.NET uses an internal optimized serialization technique for the many of the base types.

I have also fixed a bug where some or all parts of the data would not appear in the browser. This was fixed by HTML encoding the output using HttpServerUtility.HtmlEncode.

  • 10 Jan 2007 - Updated source file to include missing project

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Patrick Long
Web Developer
Europe Europe
I have been programming now for 10 years starting with COBOL, CICS and DB2 and ending up with ASP.NET, WPF, and various web technologies.

I work for Munkiisoft in the UK.

Check out my blog here

You may also be interested in...

Comments and Discussions

 
Questionwhat if I have multiple w3wp worker process for a given web app ? Pin
mrpraba9-Sep-14 3:42
membermrpraba9-Sep-14 3:42 
SuggestionWhy not a distributed cache ? Pin
wesnur24-Sep-12 21:59
memberwesnur24-Sep-12 21:59 
GeneralError getting in Charteris.Web.HttpHandlers solution Pin
yk10june19-Oct-10 23:16
memberyk10june19-Oct-10 23:16 
Generalsession end Pin
Ajay Kale New9-Sep-10 4:16
memberAjay Kale New9-Sep-10 4:16 
GeneralThanks! I am using it on Catharsis Pin
Radim Köhler25-Jun-09 21:31
memberRadim Köhler25-Jun-09 21:31 
Generallosing session when I am use Response.Redirect Pin
prasadtalla17-Jul-07 3:53
memberprasadtalla17-Jul-07 3:53 
QuestionWhat is the use of writer.Write(1) Pin
Sucharit Sengupta2-Jul-07 2:18
memberSucharit Sengupta2-Jul-07 2:18 
GeneralSession Objects Size on Remote Server Pin
netcove4-Apr-07 3:44
membernetcove4-Apr-07 3:44 
GeneralRe: Session Objects Size on Remote Server Pin
Patrick Long2-Jul-07 12:57
memberPatrick Long2-Jul-07 12:57 
QuestionQuestion Pin
codebased19-Feb-07 1:34
membercodebased19-Feb-07 1:34 
AnswerRe: Question Pin
Patrick Long20-Feb-07 10:37
memberPatrick Long20-Feb-07 10:37 
GeneralExtension Suggestion Pin
Mike Elliott24-Feb-06 8:48
memberMike Elliott24-Feb-06 8:48 
Questionhelp me with session ... Pin
tuan hai22-Nov-05 21:23
membertuan hai22-Nov-05 21:23 
GeneralSession management options in ASP.NET Pin
ElanKathir20-Oct-05 21:55
sussElanKathir20-Oct-05 21:55 
GeneralRe: Session management options in ASP.NET Pin
rohitiscancerian25-Oct-05 19:04
memberrohitiscancerian25-Oct-05 19:04 
GeneralRe: Session management options in ASP.NET Pin
Telekinetic6-Mar-06 6:03
memberTelekinetic6-Mar-06 6:03 
GeneralRe: Session management options in ASP.NET Pin
Portatofe2-Oct-08 8:36
memberPortatofe2-Oct-08 8:36 
QuestionSee size in session? Pin
coolguyrob11-Oct-04 16:14
membercoolguyrob11-Oct-04 16:14 
GeneralFormatted XML Output Pin
matzy23-Apr-04 7:37
membermatzy23-Apr-04 7:37 
GeneralRe: Formatted XML Output Pin
Patrick Long26-Apr-04 2:36
memberPatrick Long26-Apr-04 2:36 
GeneralCache Pin
matzy23-Apr-04 7:23
membermatzy23-Apr-04 7:23 
Generaldot.net/VS version? Cannot open solution! Pin
manfbraun16-Jul-03 21:08
membermanfbraun16-Jul-03 21:08 
GeneralRe: dot.net/VS version? Cannot open solution! Pin
Patrick Long16-Jul-03 23:41
memberPatrick Long16-Jul-03 23:41 
GeneralRe: dot.net/VS version? Cannot open solution! Pin
manfbraun17-Jul-03 0:50
membermanfbraun17-Jul-03 0:50 
GeneralRe: dot.net/VS version? Cannot open solution! Pin
Patrick Long17-Jul-03 21:22
memberPatrick Long17-Jul-03 21:22 
GeneralRe: dot.net/VS version? Cannot open solution! Pin
manfbraun18-Jul-03 8:42
membermanfbraun18-Jul-03 8:42 
GeneralRe: dot.net/VS version? Cannot open solution! Pin
Patrick Long20-Jul-03 5:50
memberPatrick Long20-Jul-03 5:50 
GeneralSession State unpreditable Pin
jmathew5-Jul-03 11:32
memberjmathew5-Jul-03 11:32 
GeneralRe: Session State unpreditable Pin
Patrick Long10-Jul-03 22:04
memberPatrick Long10-Jul-03 22:04 
GeneralError executing project Pin
aland@dbsoft.com3-Jun-03 8:27
memberaland@dbsoft.com3-Jun-03 8:27 
GeneralRe: Error executing project Pin
Patrick Long3-Jun-03 10:23
memberPatrick Long3-Jun-03 10:23 
QuestionSee size in cache? Pin
Paul Tallett29-May-03 22:18
memberPaul Tallett29-May-03 22:18 
AnswerRe: See size in cache? Pin
Patrick Long30-May-03 1:05
memberPatrick Long30-May-03 1:05 
GeneralRe: See size in cache? Pin
Patrick Long3-Jun-03 10:41
memberPatrick Long3-Jun-03 10:41 
GeneralRe: See size in cache? Pin
-~Chris~-29-Oct-04 10:45
member-~Chris~-29-Oct-04 10:45 
GeneralError Message opening up project Pin
JerryK@PikeOnline.net28-May-03 6:58
memberJerryK@PikeOnline.net28-May-03 6:58 
GeneralRe: Error Message opening up project Pin
Patrick Long28-May-03 10:39
memberPatrick Long28-May-03 10:39 
GeneralRe: Error Message opening up project Pin
lasthope838-Jan-07 5:30
memberlasthope838-Jan-07 5:30 
GeneralRe: Error Message opening up project Pin
Patrick Long9-Jan-07 9:54
memberPatrick Long9-Jan-07 9:54 
I have sent the updated files to codeproject.com and i will reply back as soon as i hear they are uploaded
GeneralRe: Error Message opening up project Pin
Patrick Long15-Jan-07 13:09
memberPatrick Long15-Jan-07 13:09 
GeneralRe: Error Message opening up project Pin
Patrick Long1-Jun-03 11:16
memberPatrick Long1-Jun-03 11:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 10 Jan 2007
Article Copyright 2003 by Patrick Long
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid