Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Charting: No File, No Session, No problem.

Whenever working with an image generator in an ASP.NET applications, you have difficult tradeoffs that you must face.  Usability Vs. Scalability.  Application Security Vs. Information Security, etc.  With the Infragistics WebChart many of these scenarios are taken care of with options to use Session State or the File system to temporarily store images before they are streamed out to the browser.   However, there comes a time when neither of these options will work.

The Problem

Session state has the annoying ability to fall apart should the app domain recycle during the session.  There are ways of working around this, but of course they all have tradeoffs as well.  Because of this, in some scenarios using session state my not be an option for your application. 

Disk IO on a heavily utilized web server can be a precious commodity.  Relying on the constant generation of files whose lifetime may only be a couple of seconds, can become impractical in certain scenarios.

Streaming a chart out directly to the browser upon receiving a request is the optimal solution.  Unfortunately, this isn't how images in an HTML page work.  In normal operation a page is first requested, and upon parsing the response the browser opens another request for the image whose URL was specified in the markup.  The fact that this must happen in two stages complicates things, since in order to stream out the image you must be able to generate it, which means you must be able to create a chart instance or somehow pass the required information to create it in your URL somehow. 

The Solution

To tackle this problem, a solution must be found which enables the chart to be 'recreated' when it comes time to stream it out.  This can be accomplished by hosting the chart inside of a UserControl.  UserControls are powerful objects in ASP.NET.  They can be dynamically loaded and added to existing WebForms, or otherwise processed.  In this solution we will dynamically create a UserControl which contains the instance of our chart.  During the initial phase when the page is rendered, we will add the UserControl to our web form.  In phase 2, we will use a specially crafted URL which points to an HTTP handler to dynamically create the UserControl, and stream the image out to the response stream. 

The Tradeoff

Like everything in life, there's a tradeoff.  This drawback to this solution is that it makes dynamic modifications of the chart difficult.  Because the chart must live outside of the scope of the Page, any communication between the page and the chart must be done through querystring parameters.  However, when faced with no other alternatives, using a querystring doesn't seem like such a hard thing to live with.

The Code

You can download a complete example of this code here.  The code is documented, and there is a quickstart.txt to help you understand how to use the solution.

No Comments