简体   繁体   中英

What is the best approach to handle errors in WCF service?

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 FaultException s. so the client will receive this information as a "graceful" response in the result.

eg

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM