/// <summary>
        /// Executes a <see cref="IViewQuery"/> asynchronously against a View.
        /// </summary>
        /// <typeparam name="T">The Type parameter of the result returned by the query.</typeparam>
        /// <param name="query">The <see cref="IViewQuery"/> to execute on.</param>
        /// <returns>A <see cref="Task{T}"/> that can be awaited on for the results.</returns>
        public override async Task <IViewResult <T> > ExecuteAsync <T>(IViewQueryable query)
        {
            var uri        = query.RawUri();
            var viewResult = new ViewResult <T>();

            string body;

            using (ClientConfiguration.Tracer.BuildSpan(query, CouchbaseOperationNames.RequestEncoding).Start())
            {
                body = query.CreateRequestBody();
            }

            try
            {
                Log.Debug("Sending view request to: {0}", uri.ToString());

                var content = new StringContent(body, Encoding.UTF8, MediaType.Json);

                HttpResponseMessage response;
                using (ClientConfiguration.Tracer.BuildSpan(query, CouchbaseOperationNames.DispatchToServer).Start())
                {
                    response = await HttpClient.PostAsync(uri, content).ContinueOnAnyContext();
                }

                if (response.IsSuccessStatusCode)
                {
                    using (ClientConfiguration.Tracer.BuildSpan(query, CouchbaseOperationNames.ResponseDecoding).Start())
                        using (var stream = await response.Content.ReadAsStreamAsync().ContinueOnAnyContext())
                        {
                            viewResult            = DataMapper.Map <ViewResultData <T> >(stream).ToViewResult();
                            viewResult.Success    = response.IsSuccessStatusCode;
                            viewResult.StatusCode = response.StatusCode;
                            viewResult.Message    = Success;
                        }
                }
                else
                {
                    viewResult = new ViewResult <T>
                    {
                        Success    = false,
                        StatusCode = response.StatusCode,
                        Message    = response.ReasonPhrase
                    };
                }
            }
            catch (AggregateException ae)
            {
                ae.Flatten().Handle(e =>
                {
                    ProcessError(e, viewResult);
                    Log.Error(uri.ToString(), e);
                    return(true);
                });
            }
            catch (OperationCanceledException e)
            {
                var operationContext = OperationContext.CreateViewContext(query.BucketName, uri?.Authority);
                if (_viewTimeout.HasValue)
                {
                    operationContext.TimeoutMicroseconds = _viewTimeout.Value;
                }

                ProcessError(e, operationContext.ToString(), viewResult);
                Log.Error(uri.ToString(), e);
            }
            catch (HttpRequestException e)
            {
                ProcessError(e, viewResult);
                Log.Error(uri.ToString(), e);
            }

            UpdateLastActivity();

            return(viewResult);
        }
예제 #2
0
        /// <summary>
        /// Executes a <see cref="IViewQuery"/> asynchronously against a View.
        /// </summary>
        /// <typeparam name="T">The Type parameter of the result returned by the query.</typeparam>
        /// <param name="query">The <see cref="IViewQuery"/> to execute on.</param>
        /// <returns>A <see cref="Task{T}"/> that can be awaited on for the results.</returns>
        public async Task <IViewResult <T> > ExecuteAsync <T>(IViewQueryable query)
        {
            var            uri        = query.RawUri();
            ViewResult <T> viewResult = null;

            string body;

            //using (ClientConfiguration.Tracer.BuildSpan(query, CouchbaseOperationNames.RequestEncoding).StartActive())
            //{
            body = query.CreateRequestBody();
            //}

            try
            {
                //Log.Debug("Sending view request to: {0}", uri.ToString());

                var content = new StringContent(body, Encoding.UTF8, MediaType.Json);

                HttpResponseMessage response;
                //using (ClientConfiguration.Tracer.BuildSpan(query, CouchbaseOperationNames.DispatchToServer).StartActive())
                //{
                response = await HttpClient.PostAsync(uri, content).ConfigureAwait(false);

                //}

                if (response.IsSuccessStatusCode)
                {
                    viewResult = new ViewResult <T>(
                        response.StatusCode,
                        Success,
                        await response.Content.ReadAsStreamAsync().ConfigureAwait(false)
                        //ClientConfiguration.Tracer.BuildSpan(query, CouchbaseOperationNames.ResponseDecoding).StartActive().Span
                        );
                }
                else
                {
                    viewResult = new ViewResult <T>(response.StatusCode, response.ReasonPhrase);
                }
            }
            catch (AggregateException ae)
            {
                ae.Flatten().Handle(e =>
                {
                    viewResult = CreateErrorResult <T>(e);
                    //Log.Error(uri.ToString(), e);
                    return(true);
                });
            }
            catch (OperationCanceledException e)
            {
                var operationContext = OperationContext.CreateViewContext(query.BucketName, uri?.Authority);
                if (_viewTimeout.HasValue)
                {
                    operationContext.TimeoutMicroseconds = _viewTimeout.Value;
                }

                viewResult = CreateErrorResult <T>(e, operationContext.ToString());
                //Log.Error(uri.ToString(), e);
            }
            catch (HttpRequestException e)
            {
                viewResult = CreateErrorResult <T>(e);
                //Log.Error(uri.ToString(), e);
            }

            UpdateLastActivity();

            return(viewResult);
        }