Exemplo n.º 1
0
        public override async Task <ResponseMessage> SendAsync(
            RequestMessage request,
            CancellationToken cancellationToken)
        {
            IDocumentClientRetryPolicy retryPolicyInstance = await this.GetRetryPolicyAsync(request);

            request.OnBeforeSendRequestActions += retryPolicyInstance.OnBeforeSendRequest;

            try
            {
                return(await RetryHandler.ExecuteHttpRequestAsync(
                           callbackMethod : () =>
                {
                    return base.SendAsync(request, cancellationToken);
                },
                           callShouldRetry : (cosmosResponseMessage, token) =>
                {
                    return retryPolicyInstance.ShouldRetryAsync(cosmosResponseMessage, cancellationToken);
                },
                           callShouldRetryException : (exception, token) =>
                {
                    return retryPolicyInstance.ShouldRetryAsync(exception, cancellationToken);
                },
                           diagnosticsContext : request.DiagnosticsContext,
                           cancellationToken : cancellationToken));
            }
            catch (DocumentClientException ex)
            {
                return(ex.ToCosmosResponseMessage(request));
            }
            catch (CosmosException ex)
            {
                return(ex.ToCosmosResponseMessage(request));
            }
            catch (AggregateException ex)
            {
                // TODO: because the SDK underneath this path uses ContinueWith or task.Result we need to catch AggregateExceptions here
                // in order to ensure that underlying DocumentClientExceptions get propagated up correctly. Once all ContinueWith and .Result
                // is removed this catch can be safely removed.
                AggregateException innerExceptions    = ex.Flatten();
                Exception          docClientException = innerExceptions.InnerExceptions.FirstOrDefault(innerEx => innerEx is DocumentClientException);
                if (docClientException != null)
                {
                    return(((DocumentClientException)docClientException).ToCosmosResponseMessage(request));
                }

                throw;
            }
            finally
            {
                request.OnBeforeSendRequestActions -= retryPolicyInstance.OnBeforeSendRequest;
            }
        }
Exemplo n.º 2
0
        public override async Task <ResponseMessage> SendAsync(
            RequestMessage request,
            CancellationToken cancellationToken)
        {
            using (ITrace childTrace = request.Trace.StartChild("Send Async", TraceComponent.RequestHandler, TraceLevel.Info))
            {
                request.Trace = childTrace;
                IDocumentClientRetryPolicy retryPolicyInstance = await this.GetRetryPolicyAsync(request);

                request.OnBeforeSendRequestActions += retryPolicyInstance.OnBeforeSendRequest;

                try
                {
                    return(await RetryHandler.ExecuteHttpRequestAsync(
                               callbackMethod : async(trace) =>
                    {
                        using (ITrace childTrace = trace.StartChild("Callback Method"))
                        {
                            request.Trace = childTrace;
                            return await base.SendAsync(request, cancellationToken);
                        }
                    },
                               callShouldRetry : async(cosmosResponseMessage, trace, token) =>
                    {
                        using (ITrace shouldRetryTrace = trace.StartChild("Call Should Retry"))
                        {
                            request.Trace = shouldRetryTrace;
                            return await retryPolicyInstance.ShouldRetryAsync(cosmosResponseMessage, cancellationToken);
                        }
                    },
                               callShouldRetryException : async(exception, trace, token) =>
                    {
                        using (ITrace shouldRetryTrace = trace.StartChild("Call Should Retry Exception"))
                        {
                            request.Trace = shouldRetryTrace;
                            return await retryPolicyInstance.ShouldRetryAsync(exception, cancellationToken);
                        }
                    },
                               trace : request.Trace,
                               cancellationToken : cancellationToken));
                }
                catch (DocumentClientException ex)
                {
                    return(ex.ToCosmosResponseMessage(request));
                }
                catch (CosmosException ex)
                {
                    return(ex.ToCosmosResponseMessage(request));
                }
                catch (AggregateException ex)
                {
                    // TODO: because the SDK underneath this path uses ContinueWith or task.Result we need to catch AggregateExceptions here
                    // in order to ensure that underlying DocumentClientExceptions get propagated up correctly. Once all ContinueWith and .Result
                    // is removed this catch can be safely removed.
                    AggregateException innerExceptions    = ex.Flatten();
                    Exception          docClientException = innerExceptions.InnerExceptions.FirstOrDefault(innerEx => innerEx is DocumentClientException);
                    if (docClientException != null)
                    {
                        return(((DocumentClientException)docClientException).ToCosmosResponseMessage(request));
                    }

                    throw;
                }
                finally
                {
                    request.OnBeforeSendRequestActions -= retryPolicyInstance.OnBeforeSendRequest;
                }
            }
        }