通过对接口调用可能出现的异常作出判断和处理,避免资源的浪费和占用~

 

 1 public class SvcHelper
 2 {
 3   public static void Using(T client, Action action) where T : ICommunicationObject
 4   {
 5     try
 6     {
 7       action(client);
 8       client.Close();
 9     }
10     catch (CommunicationException)
11      {
12       client.Abort();
13       throw;
14     }
15     catch (TimeoutException)
16     {
17       client.Abort();
18       throw;
19     }
20     catch (Exception)
21     {
22       client.Abort();
23       throw;
24     }
25   }
26 }

 

调用方法

var T = new XXX();
SvcHelper.Using(client =>
{
    T = client.SomeMethod();
}
return T;

 

相关文章:

  • 2022-12-23
  • 2022-03-03
  • 2022-12-23
  • 2021-07-31
  • 2022-12-23
  • 2021-09-20
  • 2022-12-23
猜你喜欢
  • 2021-09-07
  • 2021-05-11
  • 2021-06-05
  • 2022-12-23
  • 2022-01-12
  • 2022-12-23
相关资源
相似解决方案