Three for Free

Application_Error Firing Too Often, Using Server.ClearError or Context.ClearError, & You’reNot Running ASP.NET 1.1

Troubleshooting Tips

LANGUAGES: All .NET Languages



Three for Free

Application_Error Firing Too Often, Using Server.ClearError or Context.ClearError, & You're Not Running ASP.NET 1.1


By Don Kiely


It's time for another round of "Troubleshooting Tips" - a collection of miscellaneous tips that I've accumulated through writing ASP.NET code and participating in Microsoft's forums.


Application_Error Event Firing Twice or More

Putting code in the Application object's Error event in global.asax is the programmatic exception handler of last resort in an ASP.NET application. Here's a common way to write such code to grab the last exception and do something with it:


Sub Application_Error(ByVal sender As Object, _

   ByVal e As EventArgs) Handles MyBase.Error


   Dim ex As Exception = Server.GetLastError()

  ' Do something with the exception: e-mail it,

  ' put in event log, format user's hard drive, whatever.

End Sub


Cool, but sometimes the code fires twice. Why it does so depends on how you're writing the code and handling exceptions, but here are a few things that can solve the problem:

  • Add Context.ClearError to the end of the procedure. This clears the error so that it doesn't fire again.
  • Make sure that you aren't raising another exception within your Application_Error code.
  • Set AutoEventWireup to False. If it's set to True, ASP.NET wires up your events automatically. If you wire them up yourself, you might get duplicate events.
  • Check where the code that is originally executing is located in the page. You might want to put it into the page's Load event procedure and check if it's a postback. This one is a long shot, but indicates that you need to be sure you're doing events correctly.


Obviously, these suggestions can affect how other things run in your app, so be sure that whatever you do doesn't have bad side effects.


Which to Use: Server.ClearError or Context.ClearError?

If you dive into the .NET docs and explore exception handling, you'll eventually discover that there are two ways to clear the last exception thrown in your ASP.NET application: Server.ClearError and Context.ClearError. Why might you use one over the other?


The simple answer is, use whichever you like. The Server object wraps a lot of the functionality that HTTP provides to your app, and Context encapsulates information about the HTTP request. If you study the intermediate language (IL) of the two versions of ClearError, you'll find that Context.ClearError essentially just zeros out the _tempError field. Server.ClearError's IL code grabs a reference to the Context object and calls its ClearError method. So it's probably slightly more efficient to use Context.ClearError, but probably not by enough to worry about.


You're Not Running ASP.NET 1.1!

As easy as it is to set up .NET, VS.NET, and ASP.NET - usually - they can still cause some frustrating nightmares for developers who don't hold their heads just right when setting things up. One of the strangest errors is this one: "Visual Studio.NET has detected that the specified Web server is not running ASP.NET version 1.1. You will be unable to run ASP.NET Web applications or services."


That sure sounds like a VS.NET or ASP.NET configuration problem, right? So most people dutifully uninstall various .NET and VS.NET components, reboot, and then tear their hair out when it is clear that ASP.NET 1.1 is installed, but they still get the error.


It turns out that there are two situations (that we know of now, at least!) that cause this problem. One arises from a web.config file in the server's Default Web Site in IIS that uses a defaultRedirect attribute. The other arises only in Windows Server 2003 where you prohibit ASP.NET 1.1 and allow ASP.NET 1.0 in IIS's Web Service Extension list. You can read the details in these Microsoft KnowledgeBase articles:


Don Kiely is senior technology consultant for Information Insights, a business and technology consultancy in Fairbanks, AK. E-mail him at mailto:[email protected].





Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.