|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionOn 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 IdeaBoth 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 Our session and cache views are custom The configuration xml to add the <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 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 ///<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 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.
If the item’s type can be understood by the 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 Points of InterestOne gotcha that I found with developing an HistoryOK, 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
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
|
||||||||||||||||||||||