Пример #1
0
        public override async Task <ResponseMessage> SendAsync(
            RequestMessage request,
            CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            RequestOptions promotedRequestOptions = request.RequestOptions;

            if (promotedRequestOptions != null)
            {
                // Fill request options
                promotedRequestOptions.PopulateRequestOptions(request);
            }

            await this.ValidateAndSetConsistencyLevelAsync(request);

            await this.client.DocumentClient.EnsureValidClientAsync();

            await request.AssertPartitioningDetailsAsync(this.client, cancellationToken);

            this.FillMultiMasterContext(request);
            return(await base.SendAsync(request, cancellationToken));
        }
        public override Task <ResponseMessage> SendAsync(
            RequestMessage request,
            CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            RequestOptions promotedRequestOptions = request.RequestOptions;

            if (promotedRequestOptions != null)
            {
                // Fill request options
                promotedRequestOptions.PopulateRequestOptions(request);

                // Validate the request consistency compatibility with account consistency
                // Type based access context for requested consistency preferred for performance
                Cosmos.ConsistencyLevel?consistencyLevel = null;
                if (promotedRequestOptions is ItemRequestOptions)
                {
                    consistencyLevel = (promotedRequestOptions as ItemRequestOptions).ConsistencyLevel;
                }
                else if (promotedRequestOptions is QueryRequestOptions)
                {
                    consistencyLevel = (promotedRequestOptions as QueryRequestOptions).ConsistencyLevel;
                }
                else if (promotedRequestOptions is StoredProcedureRequestOptions)
                {
                    consistencyLevel = (promotedRequestOptions as StoredProcedureRequestOptions).ConsistencyLevel;
                }

                if (consistencyLevel.HasValue)
                {
                    if (!ValidationHelpers.ValidateConsistencyLevel(this.client.AccountConsistencyLevel, consistencyLevel.Value))
                    {
                        throw new ArgumentException(string.Format(
                                                        CultureInfo.CurrentUICulture,
                                                        RMResources.InvalidConsistencyLevel,
                                                        consistencyLevel.Value.ToString(),
                                                        this.client.AccountConsistencyLevel));
                    }
                }
            }

            return(this.client.DocumentClient.EnsureValidClientAsync()
                   .ContinueWith(task => request.AssertPartitioningDetailsAsync(this.client, cancellationToken))
                   .ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    throw task.Exception;
                }

                this.FillMultiMasterContext(request);
                return base.SendAsync(request, cancellationToken);
            })
                   .Unwrap());
        }