Client side exceptions catching

Mar 13, 2008 at 9:08 AM
Hi all,

I've set up a wcf service on IIS and I'm started using it from a web application.

Until recently, I used a ServiceContractClient singleton but I realized that if faulted it becomes unusable for further requests.
So I chose to instanciate a ServiceContractClient instance in each page that needs it.

One problem is that I have to wrap each and every calls sequence to the service methods with a try catch block to trap any timeout or communication error and call Abort on the ServiceContractClient instance

In short, my code looks like this:

ServiceContractClient Client = new ServiceContractClient ();
catch (TimeoutException timeout)
// Handle the timeout exception.
catch (CommunicationException commException)
// Handle the communication exception.

Is there a proper way to factorize this code in order to avoid duplicating it in each page?

Mar 13, 2008 at 12:58 PM
You can find some hints in this thread:

There are also some good samples here:

and some readings here:
Mar 13, 2008 at 1:27 PM
Thanks Charly
It seems like I need those try catch blocks for every invocation
Mar 13, 2008 at 4:29 PM
If you follow the first link, its usage is pretty simple and you shoul not need all the catch blocks since they are being handled inside the helper class.
According to one user posts at the bottom of the blog, the usage should be somthing like this;

using (CalculatorClient client = new CalculatorClient())
   throw new ApplicationException("Hope this exception was not important, because it might be masked by the Close exception.");
} // <-- this line might throw an exception.
Mar 14, 2008 at 10:28 AM
Will try
Thanks Charly