Exceptions thrown by an XML Web service method created using ASP.NET are sent back to the client in the form of a SOAP fault. A SOAP fault is a <Fault> XML element within a SOAP message that specifies when an error occurred. When passing a SOAP fault, ASP.NET follows the method prescribed for propagating errors back to a client by the SOAP specification. The SOAP <Fault> XML element contains details such as the exception string and the source of the exception. For details on SOAP faults, see the W3C Web site (http://www.w3.org/TR/SOAP).
Fortunately, both clients and XML Web services created using ASP.NET do not populate or parse the <Fault> XML element directly, but rather use the common design pattern for throwing and catching exceptions in the .NET Framework. An XML Web service can either throw an exception specific to the problem, such as an ArgumentOutOfRangeException or SoapException. Either way, ASP.NET serializes the exception into a valid SOAP message by placing the exception into a SOAP fault element. When the SOAP message is deserialized on an ASP.NET client, the SOAP fault is converted to a SoapException exception, with the exception details placed in the Message property. A client can thus set up a Try/Catch block to catch a SoapException.
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. Build a SOAP extension to process XML Web service exceptions in a global exception handler. A SOAP extension can check for the existence of an exception in the ProcessMessage method. Within the ProcessMessage method, check the Exception property of the SoapMessage passed when the Stage property is set to AfterSerialize. For details on SOAP extensions, see Altering the SOAP Message Using SOAP Extensions.
Throwing Exceptions from an XML Web Service Created Using ASP.NET
Propagating errors back to a client is done by throwing exceptions. An XML Web service method can do this four ways:
- Throw a SoapException exception.
- Throw a SoapHeaderException exception.
- Throw an exception specific to the problem.
- Allow ASP.NET to throw the exception.
The following table describes the exceptions an XML Web service can explicitly throw and how an ASP.NET client receives each exception.
| Type of Exception thrown | What an XML Web service can do |
| Exception other than SoapException and SoapHeaderException | An XML Web service method detects an exception case and throws the specific exception, such as ArgumentOutOfRangeException, back to the client. An ASP.NET client receives a SoapException with the details serialized into text in the Message property. |
| SoapException | An XML Web service method detects an exception case and throws a SoapException. It also provides additional details regarding the problem. The XML Web service method populates the Detail property to provide this additional information. An ASP.NET client receives the SoapException with the additional information. |
| SoapHeaderException | An XML Web service method detects an exception case while processing a SOAP header. The XML Web service method must throw a SoapHeaderException back to the client, according to the SOAP specification. An ASP.NET client receives the SoapHeaderException. |
To throw an exception from an XML Web service
To catch an exception thrown by an XML Web service method
Exceptions not handled by an XML Web service method
If an XML Web service method does not catch an exception that occurs within the method, the following table outlines how the exception is handled by ASP.NET.
| When unhandled exception occurs | What ASP.NET does |
| While executing the XML Web service method | The exception is caught by ASP.NET and thrown back to the client. The XML Web service client created using the .NET Framework receives a SoapException with the specific exception placed in the InnerException property. |
| While processing SOAP headers | ASP.NET throws a SoapHeaderException. An XML Web service client created using the .NET Framework receives the SoapHeaderException. |
See Also
SoapException Class | SoapHeaderException Class | Handling and Throwing Exceptions | Building XML Web Services Using ASP.NET | Building XML Web Service Clients