ASP.NET Tip: Debug mode

...

Background

ASP.NET applications let you specify if they should be run with extra debugging “stuff” turned on or off via the web.config file. Production applications should run with debug mode turned off to reduce overhead, while development and test environments should run the application in debug mode.

In addition to the built-in debugging stuff that ASP.NET does, there may be certain things that you want your code to run only when in debug mode and not while in production mode.

Example

For example, I’ve been writing a custom RSS feed generator as an .ashx file.

This “page” first checks to see if a cached version of the feed exists in server memory, if a cached version exists it outputs the cached version (the cache expiration is configurable via an integer variable passed to the method).

If a cached version does not exist it sets up the RSS header, grabs the data, and formats it as RSS Items in the feed, it then adds the newly generated feed to the server cache with an expiration set at “now” plus however many hours were passed to the method, then sends pings (in background threads) to a bunch of RDF ping servers via another method.

That’s exactly what I want it to do in the “real word” or “production environment. But in development and testing, if I make a change, I’m out of luck, I can’t see my changes until the cached feed expires.

How do I do that?

Luckily for us, ASP.NET includes a way for us to do that, built right in.

Simply surround the code you don’t want to run outside of debug mode like this:

   1: #if DEBUG
   2:     // Whatever code you put here will ONLY run when in DEBUG mode.
   3: #endif

So, I simply tell my cached version of the feed to expire now. Problem solved.

You can thank me later™.

Leave a Reply