代码之家  ›  专栏  ›  技术社区  ›  bigb055

连接超时时未捕获WebException

  •  0
  • bigb055  · 技术社区  · 6 年前

    我想抓住 网络例外 在里面有一定的地位 我是过渡性的 方法,但它从未捕获以下错误:

    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服务调用timeout时,它不工作。

    0 回复  |  直到 6 年前
    推荐文章