Repeating a function in C# until it no longer throws an exception

后端 未结 11 2075
一生所求
一生所求 2021-01-18 00:31

I\'ve got a class that calls a SOAP interface, and gets an array of data back. However, if this request times out, it throws an exception. This is good. However, I want m

相关标签:
11条回答
  • 2021-01-18 00:47

    Although I would NOT recommend you to do this for an infinite number of times, you could make a separate function out of that one sentence:

    void GoConnect()
    {
        try
        {
            salesOrdersArray = MagServ.salesOrderList(sessID, filter);
        }
        catch
        {
            GoConnect();
        }
    }
    
    0 讨论(0)
  • 2021-01-18 00:48

    I will use a transactional queue (MSMQ) to store the service call. A loop will dequeue messages and call the service in a TransactionScope, if the call fails the message appear to be still in the queue. An ov erall timeout can be specified by adding a time to expire in the message. This solution is good if you really want a reliable solution since I guessed that calling that operation is critical.

    0 讨论(0)
  • 2021-01-18 00:50
    bool repeat = true;
    while (repeat)
    {
        try
        {
           salesOrdersArray = MagServ.salesOrderList(sessID, filter);
           repeat = false;
        }
        catch
        {
        }
    }
    
    0 讨论(0)
  • 2021-01-18 00:52

    I follow this pattern in order to solve this problem:

        public void Send(String data, Int32 attemptNumber)
        {
            try
            {
                yourCodeHere(data);
            }
            catch (WebException ex)
            {
                if (attemptNumber > 0)
                    Send(data, --attemptNumber);
                else
                    throw new AttemptNumberExceededException("Attempt number exceeded!", ex);
            }
            catch (Exception ex)
            {
                //Log pourpose code goes here!
                throw;
            }
        }
    

    Trying forever seems not to be a good idea as you may end up having an infinite process. If you think you need many attempts to achieve your goal just set huge number here.

    I personally think its wise to wait some milliseconds, or seconds after eac attempt Thread.Sleep(1000); before callig Send(data); --- you could for example, use the attempNumber variable to increse or decrease this waiting time if you think its wise for your scenario.

    0 讨论(0)
  • 2021-01-18 01:04
    while(salesOrdersArray == null){
    
      try
      {
        salesOrdersArray = MagServ.salesOrderList(sessID, filter);
      }
      catch(salesOrderException e)
      {
         log(e.message);
      }
    }
    

    This will run forever, and is using exceptions as a loop which is slow. Is there a way you can modify your function that it returns null, instead of throwing an exception? If you're expecting that this call will fail regularly, don't use a try/catch block.

    0 讨论(0)
提交回复
热议问题