views:

337

answers:

3

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

A: 

hello Steven,

i think that it depends on who will use this service,

if you are the only one that will use this service i recommend you to return nothing(or the original request) and throw exceptions if something is wrong.

if the service is for public use, you should return some response object that will contain errors, and some other relevant data.

good luck

Chen Kinnrot
Hello Chen Kinnrot.Thanks for your response. I did forget to mention that this is not an internal app, and that it will be used by outside clients who may\will not be using wcf. Would you see any benefit if the response object also contained the original request, or would that be overkill?
stevenrosscampbell
i think that you don't need to return the request unless you must. if you can return just a simple string or number that will be some key for result type(you'll publish the result meaning in some site) it will be best, because you allow any technology to work with your service.
Chen Kinnrot
A: 

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.

pmarflee
Hello Pmarflee.Thanks for your response. My appologes, I forgot to mention that the this webservice is not internal, but for a client, who may\will not be using wcf. Based upon this information, would it still be possible to go with the model you described above?
stevenrosscampbell
Are you using SOAP as the protocol for transmitting messages? If so, the SOAP format defines a Fault element that can be used to hold errors and status information for a SOAP message. SOAP is XML-based, so your client will be able to read the error information contained in the fault regardless of what platform they are using. WCF exception shielding means that error information will only be propagated to the client in relation to errors that the service operation was configured to expose. Any other errors will be suppressed and a generic error message returned to the client.
pmarflee
A: 

As a general rule of thumb: if the circumstances do not allow you to complete the request successfully, throw an exception. If you want to communicate back other information, such as an order number etc. then use a return value. It is not generally a good idea to signal back a complete failure via a boolean return value - that could be ignored / not checked by the caller.

Now since it's a WCF service that is being used by outside sources, you shouldn't just throw an exception - that's a .NET specific thing - but rather throw a SOAP fault. You should declare these faults as part of your service contract on every operation your service offers, by adding one or more FaultContract attribute to your operations:

[FaultContract(typeof(MyFault1))]
[FaultContract(typeof(MyFault2))]
[OperationContract]
void MyOperation()

You can do this easily in .NET WCF by throwing a FaultException or a more specific FaultException (a generic variant in which you can specify the exakt type of fault).

Marc

marc_s