Why global.asax Application_Error method does not catch exceptions thrown by ASMX service? Why global.asax Application_Error method does not catch exceptions thrown by ASMX service? asp.net asp.net

Why global.asax Application_Error method does not catch exceptions thrown by ASMX service?


This is a known issue in .Net - Application_Error never fires for a web service. Not sure if there's any reason it would be by design, but it just doesn't work.

Jeff Atwood had a post (and follow-up) about this a few years ago, with the following ideas:

  • Put a try-catch block around each web service method
  • Use a facade design pattern and include the try-catch in parent objects
  • Write a custom SOAP extension or HTTPModule

The only one I care for is the first one, even though it seems like a lot of work.


Better: HttpApplication.Error event:

Note:

If your Web application contains XMLWeb services, you cannot use the Errorevent for global exception handling ofthose services. The HTTP handler forXML Web services consumes anyexception that occurs in an XML Webservice and converts it to a SOAPfault before the Error being called.To handle XML Web service exceptions,build a SOAP extension to process Webservice exceptions in a custom globalexception handler. For moreinformation, see Handling and ThrowingExceptions in XML Web Services.


For anyone looking for Microsoft's word on this topic, see here: "Handling and Throwing Exceptions in XML Web Services".

This is the relevant section:

A Web application can be comprised of multiple XML Web services, however the Application_Error event within the Global.asax file cannot be used for global exception handling. The HttpHandler for XML Web services consumes any exception that occurs while an XML Web service is executing and turns it into a SOAP fault prior to the Application_Error event is called.