How can I improve this exception retry scenario?

前端 未结 10 1569
暗喜
暗喜 2020-12-13 19:39

I have a web service method I am calling which is 3rd party and outside of my domain. For some reason every now and again the web service fails with a gateway timeout. Its i

10条回答
  •  余生分开走
    2020-12-13 20:29

    You can do it in a loop.

    Exception firstEx = null;
    for(int i=0; i<5; i++) 
    {
        try
        {
            MDO = OperationsWebService.MessageDownload(MI);
            firstEx = null;
            break; 
        }
        catch(Exception ex)
        {
            if (firstEx == null) 
            {
                firstEx = ex;
            }
            Thread.Sleep(100 * (i + 1));
        }
    }
    if (firstEx != null) 
    {
        throw new Exception("WebService call failed after 5 retries.", firstEx);
    }
    

提交回复
热议问题