IMPORTANT: This version of Sitefinity CMS is out of support and the respective product documentation is no longer maintained and can be outdated. Use the version selector to view a supported product version.
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":
string
test =
null
;
if
(test.Length > 0)
{
return
}
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:
try
catch
(NullReferenceException ex)
throw
new
WebProtocolException(HttpStatusCode.InternalServerError,
"GetClassInternal method threw an exception because the 'test' string field was not initialized."
,
ex);
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
To submit feedback, please update your cookie settings and allow the usage of Functional cookies.
Your feedback about this content is important