WCF - Faults / Exceptions versus Messages

前端 未结 3 2051
轻奢々
轻奢々 2020-12-07 17:12

We\'re currently having a debate whether it\'s better to throw faults over a WCF channel, versus passing a message indicating the status or the response from a service.

3条回答
  •  醉话见心
    2020-12-07 17:14

    This however carries overhead as throwing exceptions in .NET can be quite costly.

    You're serializing and de-serializing objects to XML and sending them over a slow network.. the overhead from throwing an exception is negligable compared to that.

    I usually stick to throwing exceptions, since they clearly communicate something went wrong and all webservice toolkits have a good way of handling them.

    In your sample I would throw an UnauthorizedAccessException with the message "Account Locked".

    Clarification: The .NET wcf services translate exceptions to FaultContracts by default, but you can change this behaviour. MSDN:Specifying and Handling Faults in Contracts and Services

提交回复
热议问题