0

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?

nuclear sweet
  • 1,079
  • 10
  • 27
  • Not sure why they closed without posting links to duplicate questions, but whatever. Look into delegates- thats what you need: https://msdn.microsoft.com/en-us/library/ms173171.aspx – Jeremy Barnes May 13 '15 at 15:11
  • Right now best solution i found is http://www.codeproject.com/Articles/622989/WCF-and-the-Try-Catch-Abort-Pattern Also question that pretend to duplicate mine not have no clear or good answer. – nuclear sweet May 14 '15 at 06:42

0 Answers0