1

I have multithreaded operation which performs a series of tasks. The problem i'm facing is a webservice that might return an Timeout when under heavy stress.

When under heavy stress (Timeout), I want the function to retry

Try
{
   // Do some actions here
} 
catch(WebException ex)
{
   if (// Timeout)
   { //Retry }
   else
   { // return error }
}
catch(Exception ex)
{ //return error }

From MSDN An Webexception can occur in the following situations:

  • Abort was previously called.
  • The time-out period for the request expired.
  • An error occurred while processing the request.

Question: In my exceptionhandling, how can seperate these 3 causes and single out the TimeoutExceptions without using the message?

Note: I know i could just increase the timeout to resolve the issue. But that doesn't statify my curiosity.

Thank you for your time

User999999
  • 2,500
  • 7
  • 37
  • 63

2 Answers2

1

You can inspect Status property comparing to one of the web request status codes:

try {

}
catch(WebException ex) {
    if (ex.Status == WebExceptionStatus.Timeout) {
        // Retry logic
    }
    else {
        // return error
    }
}

See also this post for an example of retry logic. Please note that Timeout isn't the only reason that should trigger a retry (IMO also ConnectionFailure and ConnectionClosed, at least).

Community
  • 1
  • 1
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
  • 1
    Sweet piece of code you've posted there. Should come in handy. thnx for helping me out. Must have missed the field ´Status´ – User999999 Sep 29 '14 at 09:00
1

You can take a look at the Status property of the Exception, which will contain the correct reason for the exception.

Since you are using some time-related behaviors, if you use some tests in your code I would recommend taking a look at the Reactive extensions; it lets you configure event chains that can allow you to easily tests desired behaviors. Look at this question for an example where I wanted some events to repeat are a higher frequency in case of an error, which may interest you.

Community
  • 1
  • 1
samy
  • 14,832
  • 2
  • 54
  • 82
  • Funny enough your post deals somewhat with the same goal we're trying to achieve. Interresting read! Will look into the possibilties `Reactive Extensions` – User999999 Sep 29 '14 at 09:04