开发者

Best practises in catching exception from wcf in winforms app

I am currently doing some research on how to handle exceptions and let the client know in a winforms app which calls a WCF service (Self-hosted in a windows service). What is the best way for this? A couple of questions:

1) If开发者_如何学C I let an exception propagate, it'll come up on the client side. 2) What's the best way to catch the exception on the client side? Is it:

catch (FaultException<T> fault) { }

(Empty catch block just for demo purposes). Or is there another way?


You will need to put each of your calls in a try{}catch{} block since that is where it will be propagated from on the client side, possibly encompassing some of the possible exception handing in some sort of proxy to hide the WCF specific handling. Using exception shielding you can also specify custom FaultExceptions and decorate the method with attributes to allow that exception to be sent down to the client. That way you can be a little more intelligent when the exception arises. e.g.

    try{
       ... call service
    }catch(FaultException<TimeoutFault> ex){
       .. try one more time
    }catch(FaultException<InvalidSelection> ex){
      ... show message to user from ex.Details.InvalidProperty
    }catch(FaultException){
      ... handle
    }catch (CommunicationException ex){
       ... remember this is WCF so the call itself might fail
    }catch(Exception ex){
     ... handle        
    }

Id reccomend reading http://blogs.msdn.com/b/pedram/archive/2008/01/25/wcf-error-handling-and-some-best-practices.aspx as it has a few good tips/


You should always at least catch CommunicationException, of which FaultException (and others) derives from. Generally you will only get FaultException during development of your service (set IncludeExceptionDetailInFaults to get better faults while debugging). CommunicationException can occur when the service is not running, among many other reasons. You may want to read this MSDN article for more information about error handling in your WCF service.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜