0

I'm acessing a webservice in a constant loop but after a few requests the server returns a 503 error which i assume that might be caused by the num of repetitive equests. (would you agree with me?)

What i would like to do is, based on the below link question, create a retry function that attemps to load the xml a couple number of times, waiting a few seconds in each attempt.

"C# cleanest way to write retry logic?"

foreach (var item in ListofItems){
XDocument.Load(URL) //this returns 
.... //handle xml data
}

The above code might return the exception described(503) and i would like to readapt the extention method defined as answer in the question that I've linked to this post.

If successfuly accomplish it should return the XmlDocument resultant from the request.

This was my attempt.

static XDocument RetryXMLLoadAction(Action action, int numRetries, int retryTimeout)
        {
            if (action == null)
                throw new ArgumentNullException("action"); // slightly safer...

            do
            {
                try { XDocument result = action(); return result; }
                catch
                {
                    if (numRetries <= 0) throw;  // improved to avoid silent failure
                    else Thread.Sleep(retryTimeout);
                }
            } while (numRetries-- > 0);
        }
Community
  • 1
  • 1
Lothre1
  • 3,523
  • 7
  • 43
  • 64
  • 3
    It seems like you are asking for a code review, in which case shouldn't this go to http://codereview.stackexchange.com/? – Daniel Kelley Jan 29 '13 at 15:38
  • maybe yes, i dind't know codereview from stackexchange. It seems recent. Thanks for the tip, anyway any help is welcome. – Lothre1 Jan 29 '13 at 15:41
  • In general, you may wish to catch the specific known exceptions (such as that 503) and let others bubble up. For example, if a 404 not found is thrown, or some security exception, you might want those thrown immediately. – Chris Sinclair Jan 29 '13 at 15:42
  • Yeah it makes sense. I'll try to keep it simple to see if i can achieve my needs. Thanks:) – Lothre1 Jan 29 '13 at 15:53

0 Answers0