如何实现重试的情况下例外,在C#中的n次?如何实现、重试、情况下

2023-09-04 22:42:46 作者:抚你发端

我开发一个WPF 4.0应用程序中,我们从远程Web服务获取数据。该Web服务公开各地的120多家方法给客户。如果从我的WPF应用程序的Web服务调用失败,我需要重试了N次也就是通过App.config中配置。如何实现这一点?是否有解决这个问题的任何设计模式?

解决方案

 静态牛逼TryNTimes< T>(Func键< T> FUNC,诠释次)
{
  而(次大于0)
  {
     尝试
     {
        返回FUNC();
     }
     赶上(例外五)
     {
       如果(--times&所述; = 0)
          扔;
     }

  }
}
 

I am developing a WPF 4.0 application in which we get the data from a remote web service. The web service exposes around 120+ methods to its clients. If a web service call from my WPF application fails, I need to retry it n times which is configurable via App.Config. How to implement this? Are there any design patterns that address this problem?

解决方案 打开另一个程序正在运行中此操作无法完成切换到重试

static T TryNTimes<T>(Func<T> func, int times)
{
  while (times>0)
  {
     try
     {
        return func();
     }
     catch(Exception e)
     {
       if (--times <= 0)
          throw;
     }

  }
}