views:

175

answers:

4

How can I determine what 'mode' my site is running under?

In this specific case, I have code in the code-behind pages that should act one way in 'release' mode - that is someone navigating there with a browser, and another way if I'm in debug mode coming from VS2008. (These are things like identifying which SQL connect string to use, whether or not to display certain error or warning messages, etc)

VS2008 is configured to go through IIS for a variety of reasons (Cassini is not an option).

Searching all through the help I can't find anything but there HAS to be a way to identify how the website was started.

Thanks in advance.

+7  A: 

I'm not sure what you mean. If you want to know if the application is currently being debugged, you can check the System.Diagnostics.Debugger.IsAttached property. If you want to know if the application is compiled in debug mode, then you can do this:

#if DEBUG
   const bool DebugMode = true;
#else
   const bool DebugMode = false;
#endif

Also, you can use ConditionalAttribute:

[System.Diagnostics.Conditional("DEBUG")]
public void ThisMethodWillOnlyExecuteInDebugMode()
{
}

I strongly suggest reconsidering that though - in my experience, having different release and debug behaviour is an excellent way to introduce bugs to your production code.

DrJokepu
+1, Pretty much how I do it
TWith2Sugars
This was the trick. Checking the System.Diagnostics.Debugger.IsAttached property tells me *exactly* what I need to know. The only place we're going to use this is for dispatching users/developers to the live/test database.
David
+1  A: 

I use this method to detect whether debug is on in the compilation section:-

bool DebugModeOn()
{
 System.Web.Configuration.CompilationSection configSection =
    (System.Web.Configuration.CompilationSection)HttpContext.Current.GetSection("system.web/compilation");
 return configSection.Debug;

}

This isn't perfect for exactly what you are asking. However since this should be false in production and true in development it may be good enough.

Edit: Or you could simply use the context's IsDebuggingEnabled property as Olivier PAYEN points out. :P oops.

AnthonyWJones
+3  A: 
HttpContext.Current.IsDebuggingEnabled

See this article

Olivier PAYEN
+1. Can't believe I've never seen that property before.
AnthonyWJones
Unfortunately, right now, that always returns "True" whether I'm hitting F5 in VS2008 or navigating manually in IE7 to https://server/app/page.aspx
David
A: 

I would use the

#if DEBUG
    // ...
#else
    // ...
#endif

approach, however, I'd only use this for Exception catching in the "Main" method:

  • If debug, then don't catch (so the debugger can catch any uncaught exceptions),
  • if release, then show an error page.

These don't change the flow of the program, just don't do that, there be dragons :)

If you look at connection strings, for example, I'd just put them in web.config.

Lennaert
The connection strings will most certainly be in the web.config - and encrypted as soon as we iron some configuration issues out. The thing we're trying to identify here is whether a developer is testing the site or a user is running it (so we know which connection string to use, as an example).
David