I have some Wcf client named MyClient
.
Lets assume it can perform N operations:
MyClient.RunA(/*some args*/)
MyClient.RunB(/*some args*/)
MyClient.RunC(/*some args*/)
MyClient.RunD(/*some args*/)
....
Number and type of arguments may vary.
For each of this operations should be wrapped in try-catch
like
using (var myClient = new MyClient(new NetTcpBinding(SecurityMode.None),
new EndpointAddress("some address goes here")))
{
try
{
myClient.RunA(/*some args*/) //(or RunB(..) and so on..)
myClient.Close();
//common success action
}
catch (TimeoutException e)
{
//common success action TimeoutException action
myClient.Abort();
}
catch (FaultException e)
{
//common success action FaultException action
myClient.Abort();
}
catch (CommunicationException e)
{
//same ...
}
....
catch (Exception e)
{
//common action
myClient.Abort();
}
}
So for each case when i need call one of this service methods, i forced to write same long try-catch
block and its only difference in method called for client.
How can i make common method, that will take client method as argument or something like that, to reduce number of this similar code parts?