Here we’ll see how the exception raised form the Web Service can be handled at the Caller level.
SoapException Information -
· Web services are limited in their ability to propagate exceptions using the Simple Object Access Protocol (SOAP).
· The .NET Framework provides the SoapException class as the only means to raise exceptions using SOAP.
· When a Web service method throws any unhandled exception, the exception is repackaged as a SoapException and is returned to the Web service client via the SOAP response.
· The ServerFaultCode is used to indicate that an error that was not a result of the message format or contents occurred during the processing of the request.
· On a .NET-based client, the Framework captures the SOAP message and deserializes the SoapException so that the client can detect exceptions through standard exception handling techniques, just as it would for a non-Web service call. However, the exception that is detected on the client is a SoapException, not the exception type that was originally thrown by the Web service.
· Information about the original exception is included in the Message property of the SoapException.
Hiding Exception Information -
· For Web services that are exposed to external companies and systems, you may not want to expose all of your exception information to your clients. You should throw an exception to your clients so that they can react.
· In this case, you should create a generic application exception that contains any information you want to convey. After catching an unhandled exception in your Web service, you should log the exception and perform any necessary processing, then construct a new instance of the generic application exception. You can then set any desired information in the generic application exception and throw it to the client. This allows you to log detailed information in the Web service and throw a less detailed exception to your clients.
Contradiction to Hiding Exception Information –
Whenever an exception [Even a fresh exception and Even a fresh Custom Exception] is thrown from the Web Service, it is throw as an ‘Soap Exception’.
e.g
System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Exception: WebService Exception at Service.HelloWorld() --- End of inner exception stack trace ---
[WebMethod]
public string HelloWorld()
{
try
{
throw new Exception("WebService Exception");
}
catch (Exception ex)
{
throw new Exception("WebService Exception"); // Or Below
throw new CustomException("Throw Custom");
}
}
public class CustomException : System.ApplicationException
{
public CustomException()
{
//
// TODO: Add constructor logic here
//
}
public CustomException(string msg)
: base(msg)
{ }
}
Thanks & Regards,
Arun Manglick || Tech Lead
No comments:
Post a Comment