WCF Exception Handling

前端 未结 3 1949
粉色の甜心
粉色の甜心 2020-12-17 00:49

If an exception occurs in my WCF service, what is the best way to communicate that error to the client?

Should I log it on the service and rethrow a soap exception?

相关标签:
3条回答
  • 2020-12-17 00:54

    An exception is supposed to be an exceptional event, so you shouldn't worry about nice messages. If exceptions only occur when you have a bug, just rethrow it as a generic exception and log it. Should not be a problem.

    However, when the exceptions occur as part of your normal process, returning nice messages would be a good idea.

    0 讨论(0)
  • 2020-12-17 01:05

    I would log it and return a FaultException. When you create the FaultException you can pass a user friendly message within it back to the client.

    0 讨论(0)
  • 2020-12-17 01:14

    Using strongly typed or non-typed FaultException is a way to accomplish this. There is an excellent article Simplifying WCF: Using Exceptions as Faults that describes how to use each. As the article points out, depending on complexity of your service you may opt to choose simpler untyped FaultException and pass exception info to the client.

    0 讨论(0)
提交回复
热议问题