[
Advertise | Submit Code | About us | Contact us | Link us
]
Go!
Membership Services
Login
Register

Home
C# General

General

C# Language

Design & Architecture

Algorithms

Database

Security

Active Directory

COM Interop

Remoting
C# Windows Forms

General

Combo and List boxes

Miscellaneous Controls

Button Controls

Edit Controls
Cutting Edge

ASP.NET 2.0

Visual Studio 2005

Windows Longhorn

SQL Server 2005
C# Multimedia and GDI+

General

DirectX

GDI+

Audio
Internet & Web

General

Images and multimedia

Database

Utilities

Security

ASP.NET Controls

Design and Architecture

Webservices
.NET

General

Design & Architecture

Algorithms

Database

Security

Active Directory

COM Interop

Remoting

ADO.NET

XML.NET

Tools

Enterprise

IDE
Visual Basic .NET

VB.NET General

VB.NET Controls
General Reading

.NET Books Review

Product Showcase

Book Chapters

Business Design & Strategy
Community

Discuss

Job Board

Discussion

CodeXchange
DeveloperLand

Advertise

Submit Code

About us

Contact us

Link us
Miscellaneous

Favorite Links

Downloads

Programming Sites

Top Stories
Regular Expressions

E-Mail

Date/Time
Home > Internet & Web > General
Failed assertion message box for ASP.NET
Posted by on Sunday, October 10, 2004 (EST)

Display Debug.Assert message boxes while debugging ASP.NET applications.

This article has been viewed: 3,488 times
Technology: General.

aspnetassert.zip (11.23 KB)

Using assertions deliberately is a good programming practice... well, but only as long as the assertion is annoying enough that it is easier to fix its cause than to ignore it. You might not agree, but this was my real-world experience with an ASP.NET application I've "inherited" recently.

The problem with the System.Diagnostics.Debug.Assert method under ASP.NET is that when the assertion fails, no message box is displayed. Instead, the assertion message and the stack trace are written into the VS.NET Output window, where it is too easy to ignore them (for some categories of developers, that is).

Because I had to spend several hours analyzing and fixing an error that could be easily avoided provided the original developer didn't ignore failed assertions, I've started to think about how to make assertions under ASP.NET more 'thorny'. My requirements were as follows:

A failed assertion in an ASP.NET application should display a message box just like the one displayed for WinForms and Console applications. Of course, the message box should be displayed only while debugging the application on local machine (i.e. the web site and the browser run on the same machine). In other circumstances, the assertion mechanism should be 'quiet'.

I've borrowed some ideas and code here and there and I've come up with an implementation that satisfies the above requirements. The usage is simple:

  1. Copy the LaMarvin.Diagnostics.AspNetAssertListener.dll assembly file to the bin subfolder of your ASP.NET application.
  2. Register the provided trace listener by inserting / modifying the trace listeners section in the web.config file of your ASP.NET application:
<system.diagnostics>
  <trace>
    <listeners>
      <add name="AspNetAssertListener"
  type="LaMarvin.Diagnostics.AspNetAssertListener,LaMarvin.Diagnostics.AspNetAssertListener"  />
    </listeners>
  </trace>
</system.diagnostics>
That's it. The next time your ASP.NET code encounters a failed assertion running locally and under debugger, a nice, annoying message box is displayed like this: 

[^] 
(click to enlarge)

If you click the Abort button, the ASP.NET worker process (aspnet_wp.exe) will be terminated, resulting in the "Server Application Unavailable" error, so in general, you should avoid using this option.

If you click the Retry button, you'll break into the debugger right at the Debug.Assert statement that failed.

If you click Cancel, the execution will continue effectively ignoring the assertion.

Here are the sources of inspiration that helped me implement the LaMarvin.Diagnostics.AspNetAssertListener assembly:

http://www.codeproject.com/aspnet/debuglistener.asp#xx399004xx [^] - An implementation of a TraceListener that rewrites the ASP.NET Response output buffer with javascript.alert code.

http://www.eggheadcafe.com/articles/20030402.asp [^] - C# implementation that uses the System.Windows.Forms.MessageBox with the MessageBoxOptions.DefaultDesktopOnly flag. (The LaMarvin.Diagnostics.AspNetAssertListener uses this approach but it doesn't reference the System.Windows.Forms assembly. Instead, I'm using Interop to invoke the MessageBox API directly - see the code for details.)

http://msdn.microsoft.com/msdnmag/issues/01/10/bugslayer/ [^] - John Robbins' article; although somewhat dated, it is a very comprehensive treatment of assertions and tracing under ASP.NET. Unfortunately, without a message box :-(

http://www.c-sharpcorner.com/Code/2003/Dec/DebugAssertInASPDotNet.asp [^] - A TraceListener descendant that just breaks into debugger depending on a custom trace switch.

Top Go to Table of Contents

About Palo Mraz

Click here if you want to know more about .

Other articles that may interest you

  • Write a Word Add-In – Part 0
  • Write a Word Add-In – Part I
  • Lengthy Operations on Single Thread in .NET Application
  • Learning Draughts
  • Exceptions and Performance
  • Average Rating :

    Discussion Forums
    Got a programming related question? Hopefully someone has the answer... Want to help out other developers? Visit our discussion forums.

    Sponsored by:

    New Articles

  • Exceptions and Performance
    Almost every time exceptions are mentioned in mailing lists and newsgroups, people say they're really expensive.Let's examine that claim, shall we?

  • Creating multilingual websites - Part 1
    Extend the existing globalization capabilities of .NET to create flexible and powerful multilingual web sites. First, create a custom ResourceManager, and then create custom localized-capable server controls to easily deploy multilingual functionality.

  • Parameter passing in C#
    Many people have become fairly confused about how parameters are passed in C#, particularly with regard to reference types. This page should help to clear up some of that confusion

  • Most Popular Articles

  • LDAP, IIS and WinNT Directory Services
    This article explains how to use .NET Directory Services to retrieve and search directory objects, create new directory objects and edit or delete existing directory objects. Describes Active Directory Application Mode (ADAM) and how to use the IIS, WinNT and LDAP directory (ADSI) provider.

  • An in-depth look at WMI and instrumentation, Part II
    WMI stands for Windows Management Instrumentation and, as the name indicates, is about managing your IT infrastructure this article is the second part of a two-part series.

  • An in-depth look at WMI and instrumentation, Part I
    WMI stands for Windows Management Instrumentation and, as the name indicates, is about managing your IT infrastructure this article provides an in-depth look at WMI and MOM 2005

  • New Books

  • Murach's ASP.NET 2.0 Upgrader's Guide: VB Edition
    What’s new and how to use it! That’s what this book delivers if you’re a VB developer who’s interested in upgrading from ASP.NET 1.x to ASP.NET 2.0.

  • C# in easy steps
    Learn to program with Microsoft’s premier programming language. No previous programming knowledge is assumed. With numerous easy-to-follow examples, this title explains the essentials of object-oriented programming with C#.

  • Murach's ASP.NET web programming with VB.NET
    Murach's ASP.NET web programming with VB.NET by Doug Lowe and Anne Prince is a in depth training and reference book for ASP.NET programming using VB.NET. The book builds upon Murach's previous books and covers more advanced concepts for programming ASP.NET pages.

  • Got Code?

    if you have any article , source code , or anything else you'd like to share with this community that you think others might find useful, please submit it here and we will gladly make it available on this site. submit@developerland.com.
    Partners

    All articles are copyrighted by their individual authors unless otherwise specified , everything else Copyright ©2004-2006 DeveloperLand