Showing posts with label WCF. Show all posts
Showing posts with label WCF. Show all posts

Monday, May 4, 2009

Bad Request in WCF REST services

While working with WCF services we tend to fall on FaultException or FaultException class to send the errors occuring on WCF server to the WCF client.
Now this works with most of the bindings on the WCF framework. However if one is using WebHttpBinding in order to expose the service as a REST service the FaultException or FaultException doesn't work. i.e. if you throw this exception from the server the client gets a Bad Request error. Now this is funny and confusing. I was testing my service by sending raw xml in http requests and when i saw a Bad Request Error i was investigating the schema of the request instead of debugging the service.
After checking the schema many times i finally changed the data in the xml and bingo it worked. And that kind of made me understand the any exception on the service results in a Bad Request exception in the client side.
Now this is not the way we'd like our exceptions to go from server to client. We want to send more specific details about what happened so that the client can take appropriate corrective measures.

One way to achieve this is to define a ResponseStatus enum and include a value of enum in the ResponseMessages from the Service Operations. This enum will keep growing as we want to send more details about the exception and then instead of throwing an exception or fault from the service set the responseStatus in the Response message which the client can use in order to take corrective actions. This obviously is an overhead since this is not the way we'd like to communicate exceptions while using Non WebHttpBindings.

Another way could be to use extensibility features of WCF to set the http status code and status description in the http response. We can intercept the response from the service just before it is sent to the client and set the details appropriately. I want to explore this option in detail if possible however for now i'd go with first approach as my customer has asked me to provide a REST service by today evening :).

~Abhishek