Capture WebService exceptions using soap extensions in ASP. NET

Source: Internet
Author: User

Application_error exceptions that cannot be caught

 
[Webmethod]Public StringHelloworld (){Throw NewException ("This exption can't be handled by application_error method");Return "Hello World";}
 
Defining application_error will not capture this exception.
 
The reason is that you need to know the lifecycle of the SOAP request in ASP. NET.
 
 

 

If the process message process calls webmethodSoapexception,SoapheaderexceptionAfter being serialized, return at the soap <fault> node.

Exceptions in the process message process are handled using the soap extended extension.
We can process soap extension.

Code 
Public   Class Soapexceptionhandler: system. Web. Services. Protocols. soapextension
{
Public   Override   Void Processmessage (system. Web. Services. Protocols. soapmessage message)
{
If (Message. Stage = Soapmessagestage. afterserialize)
{
If (Message. Exception ! =   Null )
{
Logutil. log. Error (message. Exception. innerexception );
}
}
}

Public   Override   Object Getinitializer (type servicetype)
{
Return   Null ;
}

Public   Override   Object Getinitializer (
Logicalmethodinfo methodinfo,
Soapextensionattribute)
{
Return   Null ;
}

Public   Override   Void Initialize ( Object Initializer)
{
}
}
 
Configure in the web. config system. Web Node
 Code 
< WebServices >
< Soapextensiontypes >
< Add Type = "Elplan. App. soapexceptionhandler, elplan. app" Priority = "1" Group = "High"   />
</ Soapextensiontypes >
</ WebServices >
 
 

Debugging:

If you use vs to directly run and debug asmx, it is not possible (because it is not a complete SOAP request.) You can use webservicestudio.
Address: http://www.codeplex.com/WebserviceStudio

ReferenceArticle:

using soap extensions in ASP. Net
handling and throwing exceptions in XML Web Services
how to create a global exception handler for a Web Service

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.