0

I'm trying to catch WebException with certain Status inside IsTransient method, but it never catches the following error:

System.Net.WebException: Unable to read data from the transport connection: Connection timed out. ---> System.IO.IOException: Unable to read data from the transport connection: Connection timed out. ---> System.Net.Sockets.SocketException: Connection timed out

And here's the code:

private bool IsTransient(Exception ex)
    {
        var webException = ex as WebException;
        if (webException != null)
        {
            return new[]
            {
                WebExceptionStatus.ConnectionClosed,
                WebExceptionStatus.ConnectFailure,
                WebExceptionStatus.Timeout,
                WebExceptionStatus.RequestCanceled,
                WebExceptionStatus.ReceiveFailure
            }.Contains(webException.Status);
        }

        return false;
    }

When I test it by disconnecting the internet connection on scanner device it returns true, but in production environment when web service calls timeout it's not working.

bigb055
  • 198
  • 3
  • 14
  • What status does it return? are you saying it returns no status at all? – TheGeneral Jan 26 '19 at 00:57
  • @TheGeneral well, looking at the stack trace I guess it returns Status Timeout, but I can't be 100% sure because it only happens in production environment. – bigb055 Jan 26 '19 at 01:01
  • If the status was TimeOut why would it return false? I mean it would be highly unlikely there is a bug in Contains. The thing is we cant guess for you, and its really your job to find out, who knows what it is returning only that its not what you expect. When you find this out (and if you have your heart set on doing this type of check) then you will be better able to deal with it – TheGeneral Jan 26 '19 at 01:01
  • @TheGeneral Assuming it's not Timeout where can I see what Status corresponds with the above error message? – bigb055 Jan 26 '19 at 01:17
  • The documentation doesn't not cover this, unless you read the source code which i am assuming is quite lengthy, or find someone who has blogged about the "exact" status code that arrives in every single itz bitz situation (which i doubt you will find) which is why i asked you if you knew the status code that is returned... You might just have to use trial and error and empirical evidence unfortunately, as there might be all sorts of edge cases – TheGeneral Jan 26 '19 at 01:25

0 Answers0