开发者

WCF: FaultContracts and exception hierarchies

开发者 https://www.devze.com 2023-02-19 05:44 出处:网络
My system consists of two sites, communicating between themselves using WCF. Occasionally, exceptions are thrown when one site invokes operations on its peer, and I\'m trying to decide what\'s the bes

My system consists of two sites, communicating between themselves using WCF. Occasionally, exceptions are thrown when one site invokes operations on its peer, and I'm trying to decide what's the best way to use WCF's FaultContracts in such cases.

In past projects, I was used to creating a non-trivial exception hierarchy, e.g.:

BaseSystemException
    CustomerServiceException
        CustomerNotFoundException
        BadCustomerNameException
        CustomerAlreadyExistsException
        ...
    OrderServiceException
        OrderNotFoundException
        OrderAlreadyExistsException
        ...

However, implementing such a hierarchy quickly becomes tiresome in WCF because I have to specify each concrete exception type in its own FaultContract. Now, since both services are part of the same system, there is no issue of leaking information/sensitive callstacks/etc. I want B to provide as much inf开发者_如何转开发ormation as possible in its exceptions, so that A can react accordingly. I should note that in practice, many of these exceptions are dealt with in the same way (failing the operation and notifying the user), but when designing my exception scheme, I don't want to assume identical handling for all exceptions.

Can anyone suggest an approach that doesn't require a FaultContract for each exception type? Is there an easier way? This is an internal interface, after all. Am I being too idealistic in wanting to use an exception hierarchy?


Have you looked into throwing a FaultException instead? This doesn't break your WCF service so your client can handle the exception.

EDIT:

As you want to keep the type of exception I don't think there is a way out of using FaultContracts. The only thing I can suggest is to consolidate some of your exceptions.

e.g CustomerNotFoundException and BadCustomerNameException could become FailedToFetchCustomerException and then the message can convey the reason why.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号