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

后端 未结 4 795
悲&欢浪女
悲&欢浪女 2020-12-04 22:11

I have a WCF service deployed on two or more remote machines and there is a desktop based application that is used by the client to access any wcf service.

T

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-04 22:58

    You can definitely catch and handle all exceptions that happen on your service class and turn them into a FaultException or FaultException exception.

    That way, you won't "fault" (or tear down) the communications channel between your client and server.

    Even better approach would be to implement the IErrorHandler interface on your service class that provides a way to globally catch all exceptions as they happen and provide a FaultException instead, that's SOAP compliant.

    You can even turn your IErrorHandler into a configurable behavior that can be turned on or off in config.

    See these articles and blog posts for more details:

    • Rory Primrose: Implementing IErrorHandler
    • Useful WCF behaviors: IErrorHandler

提交回复
热议问题