1

I have read this question: What is the best approach to handle exceptions in WCF service?

Which answers how to handle exceptions.

I have seen few services that return a MyServiceResult class containing both Data and an Error object (if any) for every method. and never throw FaultExceptions. so the client will receive this information as a "graceful" response in the result.

e.g.

[OperationContract]
MyServiceResult Login(string userName, string password);

My question which is a better approach to handle errors and why?

Community
  • 1
  • 1
zig
  • 4,524
  • 1
  • 24
  • 68
  • I think Faultcontract is better.....In case of unhandled exception on server side, your second approach may fail. – Viru Jan 19 '16 at 12:06
  • @Viru, How can there be an unhandled exception on the server if every method is warped in try/catch and handles any exception? – zig Jan 19 '16 at 15:33
  • Did I answer your questions? – Artyom Jan 22 '16 at 06:10

1 Answers1

2

This question is similar to what is the best approach to handle errors in a called method, in a usual inner-process execution. The answer is it depends on what is an error you expect. Meaning what is an exception (some breaking unexpected by method behavior), what are those errors that can be handled. I believe exceptions should not create program workflow. And so throwing WCF FaultException for it is bad to my mind. In this case as you mentioned the returning some result object with expected errors can work.

For further reading about this I strongly advise you to check Steve McConnell Code Complete book, Ch.8.3 Error-Handling Techniques and 8.4 Exceptions. I believe WCF does not change rules much here.

Artyom
  • 3,507
  • 2
  • 34
  • 67