Home > .NET Framework > ASP.NET Web Services: Exception Handling

ASP.NET Web Services: Exception Handling

Web Service end:
The main question being whether we should throw custom Exceptions and let the ASP.NET Framework map this into the SOAP fault infrastructure or should we directly use the infrastructure provided by SoapException do the SOAP fault mapping ourselves. In code
[WebMethod]
public string HelloWorld()
{
throw new Exception("Not yet implemented");
}

is right or

[WebMethod]
public string HelloWorld2()
{
SoapFaultSubCode appSpecificSubCode = new SoapFaultSubCode(new XmlQualifiedName("NotImpl", "http://tempuri.org"));
throw new SoapException("Not yet implemented", Soap12FaultCodes.ReceiverFaultCode, appSpecificSubCode);
}
is right
Its good if the mapping is done by the Framework itself and you do not worry about the SOAP version, protocol rules etc. This will make sure your code will hold good for SOAP 1.3 too. So the initial hunch will take you towards HelloWorld1 option.
<soap:Fault>
<soap:Code>
<soap:Value>soap:Receiver</soap:Value>
</soap:Code>
<soap:Reason>
<soap:Text xml:lang="en">
System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Exception: Not yet implemented
at Soap20ExtensionsServiceApplication.Service1.HelloWorld() in D:SendhilresSoapExtensionsSoap20ExtensionsServiceApplicationService1.asmx.cs:line 24
--- End of inner exception stack trace ---
</soap:Text>
</soap:Reason>
<soap:Detail />
</soap:Fault>
 
So Helloworld1 client can hardly do something about this response. As per the SOAP Specification the application specific fault information (for algorithmic / machine processing) goes into the Subcode element, which is missing here. The Reason element is human readable explanation of the fault. So the in built mapping is pretty much useless if you have a programatic client (which is true in 99.99% of cases, after all services are machine to machine interactions)
What happens if we use the second option?
<soap:Fault>
<soap:Code>
<soap:Value>soap:Receiver</soap:Value>
<soap:Subcode>
<soap:Value xmlns:q0="http://tempuri.org">q0:NotImpl</soap:Value>
</soap:Subcode>
</soap:Code>
<soap:Reason>
<soap:Text xml:lang="en">
System.Web.Services.Protocols.SoapException: Not yet implemented
at Soap20ExtensionsServiceApplication.Service1.HelloWorld2() in D:SendhilresSoapExtensionsSoap20ExtensionsServiceApplicationService1.asmx.cs:line 31
</soap:Text>
</soap:Reason>
<soap:Detail />
</soap:Fault>
 
Which is exactly what we want, the SOAP fault SubCode provides machine readable information whereas the Reason provides a human readble information. If you want to sepcify the some other detail related to fault code (like when this service will be implemented) can be specified in the Detail element (Using the XmlNode overload of SoapException class)
 
That’s about throwing exception from the web service itself. But what about exceptions thrown from layers below. You should probably catch them and throw a SoapException for reasons mentioned above, Security reasons (you do not want to return your whole stack trace to the external world) and you/or may want to throw a high level user friendly message. Do we need to have try catch blocks for all the web services? Not necessarily. A SoapExtension should do the job. What do we do on the client side? Well that calls for a separate post…

Advertisements
Categories: .NET Framework
  1. Unknown
    November 23, 2008 at 10:06 am

    您需要二手液晶显示屏、废旧液晶屏么?我们是不折不扣的二手液晶屏、旧液晶屏大批发商,长期大量供应可再利用的旧液晶屏。我公司提供的各种尺寸的二手液晶屏, 不同厚薄如笔记本屏,均已经过我们严格的分类,检验,测试流程。请访问协力液晶屏www.sceondhandlcd.com[bbibbfeeggfgaeca]

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: