Original: Capturing WebService exceptions using SOAP extensions in ASP.
Application_Error exceptions that cannot be caught
[WebMethod] Public string HelloWorld () { thrownew Exception ("This exption can ' t is handled by Application_ Error Method "); return "Hello World"; }
Defining Application_Error will not catch this exception.
Reason needs to understand the lifetime of the SOAP request in the ASP.
If SoapExceptionis generated when the process message procedure calls WebMethod,SoapHeaderException is serialized and returned in the soap <Fault> node.
Exceptions generated by process message procedures using the SOAP extension extension
We can handle the SOAP extension.
Code
Public classSoapExceptionHandler:System.Web.Services.Protocols.SoapExtension
{
Public Override voidProcessMessage (System.Web.Services.Protocols.SoapMessage message)
{
if(message. Stage==soapmessagestage.afterserialize)
{
if(message. Exception!= NULL)
{
LogUtil.Log.Error (message. Exception.innerexception);
}
}
}
Public Override ObjectGetInitializer (Type servicetype)
{
return NULL;
}
Public Override ObjectGetInitializer (
LogicalMethodInfo MethodInfo,
SoapExtensionAttribute attribute)
{
return NULL;
}
Public Override voidInitialize (Objectinitializer)
{
}
}
configuring in the Web. config system.web node
Code
<webservices>
<soapExtensionTypes>
<Addtype= "Elplan.App.SoapExceptionHandler, Elplan.app" Priority= "1"Group= "High" />
</soapExtensionTypes>
</webservices>
Debugging:
It is not possible to run debug asmx directly with vs. (Because ...) It is not a complete SOAP request.) Webservicestudio can be used.
Related address: Http://www.codeplex.com/WebserviceStudio
Reference article:
Using SOAP Extensions in ASP.
Handling and throwing Exceptions in XML Web Services
How to create a global exception handler for a Web Service
Capturing WebService exceptions using SOAP extensions in ASP.