连接超时时未捕获WebException

时间:2019-01-26 00:53:53

标签: c# error-handling exception-handling system.net.webexception retry-logic

我正在尝试在 IsTransient 方法内捕获具有特定Status的 WebException ,但它从未捕获到出现以下错误:

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

这是代码:

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;
    }

当我通过断开扫描仪设备上的Internet连接来进行测试时,它返回true,但是在生产环境中,当Web服务调用超时时,它不起作用。

0 个答案:

没有答案