OnUnexpectedException() 공개 메소드

public OnUnexpectedException ( Exception unexpectedException ) : void
unexpectedException System.Exception
리턴 void
예제 #1
0
        GetHttpWebResponseStreamWithRetries
        (
            String url,
            HttpStatusCode [] httpStatusCodesToFailImmediately,
            RequestStatistics requestStatistics,
            String userAgent,
            Int32 timeoutMs,
            ReportProgressHandler reportProgressHandler,
            CheckCancellationPendingHandler checkCancellationPendingHandler
        )
        {
            Debug.Assert(!String.IsNullOrEmpty(url));
            Debug.Assert(requestStatistics != null);
            Debug.Assert(!String.IsNullOrEmpty(userAgent));
            Debug.Assert(timeoutMs > 0);

            Int32 iMaximumRetries = HttpRetryDelaysSec.Length;
            Int32 iRetriesSoFar   = 0;

            while (true)
            {
                if (reportProgressHandler != null && iRetriesSoFar > 0)
                {
                    reportProgressHandler("Retrying request.");
                }

                // Important Note: You cannot use the same HttpWebRequest object
                // for the retries.  The object must be recreated each time.

                HttpWebRequest oHttpWebRequest = CreateHttpWebRequest(
                    url, userAgent, timeoutMs);

                Stream oStream = null;

                try
                {
                    if (checkCancellationPendingHandler != null)
                    {
                        checkCancellationPendingHandler();
                    }

                    oStream = GetHttpWebResponseStreamNoRetries(oHttpWebRequest);

                    if (reportProgressHandler != null && iRetriesSoFar > 0)
                    {
                        reportProgressHandler("Retry succeeded, continuing...");
                    }

                    requestStatistics.OnSuccessfulRequest();

                    return(oStream);
                }
                catch (Exception oException)
                {
                #if WriteRequestsToDebug
                    Debug.WriteLine("Exception: " + oException.Message);
                #endif

                    if (oStream != null)
                    {
                        oStream.Close();
                    }

                    // The IOException "Unable to read data from the transport"
                    // connection: The connection was closed." can occur when
                    // requesting data from Twitter, for example.

                    if (!(
                            oException is WebException
                            ||
                            oException is IOException
                            ))
                    {
                        throw oException;
                    }

                    if (iRetriesSoFar == iMaximumRetries)
                    {
                        requestStatistics.OnUnexpectedException(oException);

                        throw (oException);
                    }

                    // If the status code is one of the ones specified in
                    // httpStatusCodesToFailImmediately, rethrow the exception
                    // without retrying the request.

                    if (httpStatusCodesToFailImmediately != null &&
                        oException is WebException)
                    {
                        if (WebExceptionHasHttpStatusCode(
                                (WebException)oException,
                                httpStatusCodesToFailImmediately))
                        {
                            throw (oException);
                        }
                    }

                    Int32 iSeconds = HttpRetryDelaysSec[iRetriesSoFar];

                    if (reportProgressHandler != null)
                    {
                        reportProgressHandler(String.Format(

                                                  "Request failed, pausing {0} {1} before retrying..."
                                                  ,
                                                  iSeconds,
                                                  StringUtil.MakePlural("second", iSeconds)
                                                  ));
                    }

                    System.Threading.Thread.Sleep(1000 * iSeconds);
                    iRetriesSoFar++;
                }
            }
        }
    GetHttpWebResponseStreamWithRetries
    (
        String url,
        HttpStatusCode [] httpStatusCodesToFailImmediately,
        RequestStatistics requestStatistics,
        String userAgent,
        Int32 timeoutMs,
        ReportProgressHandler reportProgressHandler,
        CheckCancellationPendingHandler checkCancellationPendingHandler
    )
    {
        Debug.Assert( !String.IsNullOrEmpty(url) );
        Debug.Assert(requestStatistics != null);
        Debug.Assert( !String.IsNullOrEmpty(userAgent) );
        Debug.Assert(timeoutMs > 0);

        Int32 iMaximumRetries = HttpRetryDelaysSec.Length;
        Int32 iRetriesSoFar = 0;

        while (true)
        {
            if (reportProgressHandler != null && iRetriesSoFar > 0)
            {
                reportProgressHandler("Retrying request.");
            }

            // Important Note: You cannot use the same HttpWebRequest object
            // for the retries.  The object must be recreated each time.

            HttpWebRequest oHttpWebRequest = CreateHttpWebRequest(
                url, userAgent, timeoutMs);

            Stream oStream = null;

            try
            {
                if (checkCancellationPendingHandler != null)
                {
                    checkCancellationPendingHandler();
                }

                oStream = GetHttpWebResponseStreamNoRetries(oHttpWebRequest);

                if (reportProgressHandler != null && iRetriesSoFar > 0)
                {
                    reportProgressHandler("Retry succeeded, continuing...");
                }

                requestStatistics.OnSuccessfulRequest();

                return (oStream);
            }
            catch (Exception oException)
            {
                #if WriteRequestsToDebug

                Debug.WriteLine("Exception: " + oException.Message);

                #endif

                if (oStream != null)
                {
                    oStream.Close();
                }

                // The IOException "Unable to read data from the transport"
                // connection: The connection was closed." can occur when
                // requesting data from Twitter, for example.

                if ( !(
                    oException is WebException
                    ||
                    oException is IOException
                    ) )
                {
                    throw oException;
                }

                if (iRetriesSoFar == iMaximumRetries)
                {
                    requestStatistics.OnUnexpectedException(oException);

                    throw (oException);
                }

                // If the status code is one of the ones specified in
                // httpStatusCodesToFailImmediately, rethrow the exception
                // without retrying the request.

                if (httpStatusCodesToFailImmediately != null &&
                    oException is WebException)
                {
                    if ( WebExceptionHasHttpStatusCode(
                            (WebException)oException,
                            httpStatusCodesToFailImmediately) )
                    {
                        throw (oException);
                    }
                }

                Int32 iSeconds = HttpRetryDelaysSec[iRetriesSoFar];

                if (reportProgressHandler != null)
                {
                    reportProgressHandler( String.Format(

                        "Request failed, pausing {0} {1} before retrying..."
                        ,
                        iSeconds,
                        StringUtil.MakePlural("second", iSeconds)
                        ) );
                }

                System.Threading.Thread.Sleep(1000 * iSeconds);
                iRetriesSoFar++;
            }
        }
    }