开发者

How to intercept WCF errors on the client side

On the server side there is IErrorHandler etc.

On the client, exceptions are throw开发者_Go百科n when you call the methods on the channel.

Is there a way to intercept the exceptions on the client before they are thrown during invocation of method?


No, you cannot intercept the exceptions before they happen.... or what are you really trying to achieve??

What you need to do on the client side is basically just defensive programming: put your service calls into try...catch... blocks and handle the exceptions you can (and want to) handle:

MyServiceClient client = new MyServiceClient();

try
{
    client.MakeYourCall();
}
catch(EndpointNotFoundException ex)
{
   // tell the user the endpoint doesn't exist
}      
catch(CommunicationException ex)
{
   // catch the WCF base exception to handle all other WCF related issues, if needed
}      
catch(TimeoutException ex)
{
   // tell the user a timeout occured
}      

All WCF related exceptions are descendants of the base CommunicationException class (except, as I recently learned: QuotaExceededException) - catch the more specific ones if you can react to them.

And then there's the system-wide TimeoutException which might also be something you want to catch (it's not WCF specific, so it doesn't descend from CommunicationException)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜