Hello and thanks for your opinion.
I am creating a webservice.
This webservice will accept a customer and the customers accounts along with a couple other related objects and attributes and such.
When the webservice recieves a request, I attempt to process it.
If there is no error I simply just return. If there is an error, I throw it.
I’m wondering if this is the best approach, or if I should modify my design so that I return a bool as the response, or even create a response object, that maybe contains the original request object plus a status and if error, a list of errors (ie: Missing Fields, Invalid Fields, ext).
Which return method would you incorporate in your design?
1) Just return if no error, Throw error if error
2) bool success
3) Response object (containing original request object? and detailed results)?
Thanks for any suggestions.
Steven
I would say that it depends on what the ‘error’ is. If the error is validation-related but expected in the normal course of events e.g invalid user credentials being passed to a service that provides user authentication, then returning an enum value that indicates the request result (Success, Failure, etc) is probably OK. However if the error is due to input that should not happen in the normal course of events, then I would suggest you throw a fault from the service operation. Throwing a fault indicates that the client app is in error and has provided input that the service does not know how to deal with. You can register particular fault contracts with a WCF service operation so a client app knows the operation may throw one of a number of faults under particular circumstances. The client app should provide exception handlers for these faults and take action if one is thrown, otherwise it will terminate unexpectedly.
As a basic rule of thumb, I would say throw faults under exceptional circumstances e.g. where the client has provided input that the service operation does not expect to receive. If the client has provided information that is incorrect but the service operation is progammed to handle, return a status code or information in the response message.