using System; using System; using System.ServiceModel; /// /// Helper class for WCF clients /// public class WcfClientUtils { /// /// Execute actions against the client and aborts the client on a faulted state /// /// Client type /// Client instance /// Actions to perform on instance /// /// WcfClientUtils.Execute(new ServiceReference1.Service1Client(), /// client => /// { /// var returnData = client.HelloWorld()); /// }); /// public static void Execute(T client, Action action) where T : ICommunicationObject { try { //client.Open(); action(client); client.Close(); } catch (CommunicationException) { client.Abort(); throw; } catch (TimeoutException) { client.Abort(); throw; } catch (Exception) { if (client.State == CommunicationState.Faulted) { client.Abort(); } throw; } } }
And this is how you could use this class
WcfClientUtils.Execute(new ServiceReference1.Service1Client(), client => { var returnData = client.HelloWorld()); });
0 kommentarer:
Post a Comment