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 ();
try
{
Client.Operation1();
Client.Operation2();
...
Client.Close();
}
catch (TimeoutException timeout)
{
// Handle the timeout exception.
Client.Abort();
}
catch (CommunicationException commException)
{
// Handle the communication exception.
Client.Abort();
}


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

Thanks
Vincent
Developer
Mar 13, 2008 at 12:58 PM
You can find some hints in this thread: http://www.codeplex.com/servicefactory/Thread/View.aspx?ThreadId=23684

There are also some good samples here:
http://bloggingabout.net/blogs/erwyn/archive/2006/12/09/WCF-Service-Proxy-Helper.aspx

and some readings here:
http://blogs.msdn.com/wenlong/archive/2007/10/27/performance-improvement-of-wcf-client-proxy-creation-and-best-practices.aspx
http://msdn2.microsoft.com/en-us/library/ms733912.aspx
Mar 13, 2008 at 1:27 PM
Thanks Charly
It seems like I need those try catch blocks for every invocation
Developer
Mar 13, 2008 at 4:29 PM
Vincent,
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