Exception handling is an important concept regardless of the implementation pattern, however, due to the specific nature of the web service programming it appears to be an invaluable practice when developing web services. Namely, since the requests to web services are more often than not will be made asynchronously through Ajax calls, the failure in the service will be invisible to the user. Even more, the exception messages may be very vague and next to useless for debugging purposes. Let us consider following code, which is going to throw an "Object not set to a reference exception":
(test.Length > 0)
When we make a request to a service method containing this piece of code, we will receive following response with the HTTP status code 500 (InternalServerError):
When debugging, it may be challenging to track the code that caused this exception even in a most trivial scenarios. In order to overcome this issue, we ought to throw WebProtocolException with more information. Let us rewrite the above code as follows:
"GetClassInternal method threw an exception because the 'test' string field was not initialized."
This time, when the service method containing the code is invoked, the response will be much more informative and allow us to find out the problem much sooner.
Needless to say, when invoking web services through AJAX calls, failures should be handled in a predictable and visible manner. ClientManager client class that comes with Sitefinity CMS does this out of the box.
Back To Top