コード例 #1
0
        //*************************************************************************
        //  Constructor: PartialNetworkException()
        //
        /// <summary>
        /// Initializes a new instance of the <see
        /// cref="PartialNetworkException" /> class.
        /// </summary>
        ///
        /// <param name="partialNetwork">
        /// The partial network that was obtained.
        /// </param>
        ///
        /// <param name="requestStatistics">
        /// Information about the requests that were made while getting the
        /// network.
        /// </param>
        //*************************************************************************

        public PartialNetworkException
        (
            XmlDocument partialNetwork,
            RequestStatistics requestStatistics
        )
        {
            m_oPartialNetwork    = partialNetwork;
            m_oRequestStatistics = requestStatistics;

            AssertValid();
        }
コード例 #2
0
        OnNetworkObtainedWithoutTerminatingException
        (
            GraphMLXmlDocument oGraphMLXmlDocument,
            RequestStatistics oRequestStatistics
        )
        {
            Debug.Assert(oGraphMLXmlDocument != null);
            Debug.Assert(oRequestStatistics != null);
            AssertValid();

            if (oRequestStatistics.UnexpectedExceptions > 0)
            {
                // The network is partial.

                throw new PartialNetworkException(oGraphMLXmlDocument,
                                                  oRequestStatistics);
            }
        }
コード例 #3
0
        GetXmlDocumentWithRetries
        (
            String sUrl,
            HttpStatusCode [] aeHttpStatusCodesToFailImmediately,
            RequestStatistics oRequestStatistics,
            params String[] asOptionalHeaderNameValuePairs
        )
        {
            Debug.Assert(!String.IsNullOrEmpty(sUrl));
            Debug.Assert(oRequestStatistics != null);

            Debug.Assert(asOptionalHeaderNameValuePairs == null ||
                         asOptionalHeaderNameValuePairs.Length % 2 == 0);

            AssertValid();

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

            while (true)
            {
                if (iRetriesSoFar > 0)
                {
                    ReportProgress("Retrying request.");
                }

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

                HttpWebRequest oHttpWebRequest =
                    (HttpWebRequest)WebRequest.Create(sUrl);

                Int32 iHeaderNamesAndValues =
                    (asOptionalHeaderNameValuePairs == null) ?
                    0 : asOptionalHeaderNameValuePairs.Length;

                for (Int32 i = 0; i < iHeaderNamesAndValues; i += 2)
                {
                    String sHeaderName  = asOptionalHeaderNameValuePairs[i + 0];
                    String sHeaderValue = asOptionalHeaderNameValuePairs[i + 1];

                    Debug.Assert(!String.IsNullOrEmpty(sHeaderName));
                    Debug.Assert(!String.IsNullOrEmpty(sHeaderValue));

                    oHttpWebRequest.Headers[sHeaderName] = sHeaderValue;
                }

                try
                {
                    XmlDocument oXmlDocument =
                        GetXmlDocumentNoRetries(oHttpWebRequest);

                    if (iRetriesSoFar > 0)
                    {
                        ReportProgress("Retry succeeded, continuing...");
                    }

                    oRequestStatistics.OnSuccessfulRequest();

                    return(oXmlDocument);
                }
                catch (Exception oException)
                {
                    if (!ExceptionIsWebOrXml(oException))
                    {
                        throw oException;
                    }

                    // A WebException or XmlException has occurred.

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

                        throw (oException);
                    }

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

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

                    Int32 iSeconds = HttpRetryDelaysSec[iRetriesSoFar];

                    ReportProgress(String.Format(

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

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