Esempio n. 1
0
        internal override async Task <ContainerProperties> GetCachedContainerPropertiesAsync(
            string containerUri,
            ITrace trace,
            CancellationToken cancellationToken)
        {
            using (ITrace childTrace = trace.StartChild("Get Container Properties", TraceComponent.Transport, Tracing.TraceLevel.Info))
            {
                this.ThrowIfDisposed();
                CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContextCore.Create(requestOptions: null);
                using (diagnosticsContext.GetOverallScope())
                {
                    ClientCollectionCache collectionCache = await this.DocumentClient.GetCollectionCacheAsync();

                    try
                    {
                        using (diagnosticsContext.CreateScope("ContainerCache.ResolveByNameAsync"))
                        {
                            return(await collectionCache.ResolveByNameAsync(
                                       HttpConstants.Versions.CurrentVersion,
                                       containerUri,
                                       forceRefesh : false,
                                       cancellationToken));
                        }
                    }
                    catch (DocumentClientException ex)
                    {
                        throw CosmosExceptionFactory.Create(ex, diagnosticsContext);
                    }
                }
            }
        }
        public override async Task <ItemResponse <T> > CreateItemAsync <T>(
            T item,
            PartitionKey?partitionKey           = null,
            ItemRequestOptions requestOptions   = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContext.Create(requestOptions);

            using (diagnosticsContext.GetOverallScope())
            {
                Task <ResponseMessage> response = this.ExtractPartitionKeyAndProcessItemStreamAsync(
                    partitionKey: partitionKey,
                    itemId: null,
                    item: item,
                    operationType: OperationType.Create,
                    requestOptions: requestOptions,
                    diagnosticsContext: diagnosticsContext,
                    cancellationToken: cancellationToken);

                return(await this.ClientContext.ResponseFactory.CreateItemResponseAsync <T>(response));
            }
        }
        internal override async Task <ContainerProperties> GetCachedContainerPropertiesAsync(
            string containerUri,
            CancellationToken cancellationToken)
        {
            this.ThrowIfDisposed();
            CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContextCore.Create(requestOptions: null);

            using (diagnosticsContext.GetOverallScope())
            {
                ClientCollectionCache collectionCache = await this.DocumentClient.GetCollectionCacheAsync();

                try
                {
                    using (diagnosticsContext.CreateScope("ContainerCache.ResolveByNameAsync"))
                    {
                        return(await collectionCache.ResolveByNameAsync(
                                   HttpConstants.Versions.CurrentVersion,
                                   containerUri,
                                   cancellationToken));
                    }
                }
                catch (DocumentClientException ex)
                {
                    throw CosmosExceptionFactory.Create(ex, diagnosticsContext);
                }
            }
        }
 /// <summary>
 /// Get the next set of results from the cosmos service
 /// </summary>
 /// <param name="cancellationToken">(Optional) <see cref="CancellationToken"/> representing request cancellation.</param>
 /// <returns>A query response from cosmos service</returns>
 public override async Task<ResponseMessage> ReadNextAsync(CancellationToken cancellationToken = default)
 {
     CosmosDiagnosticsContext diagnostics = CosmosDiagnosticsContext.Create(this.requestOptions);
     using (diagnostics.GetOverallScope())
     {
         return await this.ReadNextInternalAsync(diagnostics, cancellationToken);
     }
 }
        /// <summary>
        /// Get the next set of results from the cosmos service
        /// </summary>
        /// <param name="cancellationToken">(Optional) <see cref="CancellationToken"/> representing request cancellation.</param>
        /// <returns>A query response from cosmos service</returns>
        public override Task <ResponseMessage> ReadNextAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            CosmosDiagnosticsContext diagnostics = CosmosDiagnosticsContext.Create(this.changeFeedOptions);

            using (diagnostics.GetOverallScope())
            {
                return(this.ReadNextInternalAsync(diagnostics, cancellationToken));
            }
        }
        /// <summary>
        /// Get the next set of results from the cosmos service
        /// </summary>
        /// <param name="cancellationToken">(Optional) <see cref="CancellationToken"/> representing request cancellation.</param>
        /// <returns>A query response from cosmos service</returns>
        public override async Task <ResponseMessage> ReadNextAsync(CancellationToken cancellationToken = default)
        {
            CosmosDiagnosticsContext diagnostics = CosmosDiagnosticsContext.Create(this.changeFeedOptions);

            using (diagnostics.GetOverallScope())
            {
                diagnostics.AddDiagnosticsInternal(new FeedRangeStatistics(this.changeFeedOptions.FeedRange));
                if (!this.lazyContainerRid.ValueInitialized)
                {
                    using (diagnostics.CreateScope("InitializeContainerResourceId"))
                    {
                        TryCatch <string> tryInitializeContainerRId = await this.lazyContainerRid.GetValueAsync(cancellationToken);

                        if (!tryInitializeContainerRId.Succeeded)
                        {
                            if (!(tryInitializeContainerRId.Exception.InnerException is CosmosException cosmosException))
                            {
                                throw new InvalidOperationException("Failed to convert to CosmosException.");
                            }

                            return(cosmosException.ToCosmosResponseMessage(
                                       new RequestMessage(
                                           method: null,
                                           requestUri: null,
                                           diagnosticsContext: diagnostics)));
                        }
                    }

                    if (this.FeedRangeContinuation == null)
                    {
                        using (diagnostics.CreateScope("InitializeContinuation"))
                        {
                            await this.InitializeFeedContinuationAsync(cancellationToken);
                        }
                    }

                    TryCatch validateContainer = this.FeedRangeContinuation.ValidateContainer(this.lazyContainerRid.Result.Result);
                    if (!validateContainer.Succeeded)
                    {
                        return(CosmosExceptionFactory
                               .CreateBadRequestException(
                                   message: validateContainer.Exception.InnerException.Message,
                                   innerException: validateContainer.Exception.InnerException,
                                   diagnosticsContext: diagnostics)
                               .ToCosmosResponseMessage(
                                   new RequestMessage(
                                       method: null,
                                       requestUri: null,
                                       diagnosticsContext: diagnostics)));
                    }
                }

                return(await this.ReadNextInternalAsync(diagnostics, cancellationToken));
            }
        }
        private async Task <TResult> RunWithDiagnosticsHelperAsync <TResult>(
            string operationName,
            RequestOptions requestOptions,
            Func <CosmosDiagnosticsContext, Task <TResult> > task)
        {
            CosmosDiagnosticsContext diagnosticsContext = this.CreateDiagnosticContext(
                operationName,
                requestOptions);

            using (diagnosticsContext.GetOverallScope())
            {
                return(await task(diagnosticsContext).ConfigureAwait(false));
            }
        }
Esempio n. 8
0
 private async Task <TResult> RunWithDiagnosticsHelperAsync <TResult>(
     CosmosDiagnosticsContext diagnosticsContext,
     Func <CosmosDiagnosticsContext, Task <TResult> > task)
 {
     try
     {
         using (diagnosticsContext.GetOverallScope())
         {
             return(await task(diagnosticsContext).ConfigureAwait(false));
         }
     }
     catch (OperationCanceledException oe) when(!(oe is CosmosOperationCanceledException))
     {
         throw new CosmosOperationCanceledException(oe, diagnosticsContext);
     }
 }
Esempio n. 9
0
        /// <summary>
        /// Get the next set of results from the cosmos service
        /// </summary>
        /// <param name="cancellationToken">(Optional) <see cref="CancellationToken"/> representing request cancellation.</param>
        /// <returns>A query response from cosmos service</returns>
        public override async Task <ResponseMessage> ReadNextAsync(CancellationToken cancellationToken = default)
        {
            CosmosDiagnosticsContext diagnostics = CosmosDiagnosticsContext.Create(this.queryRequestOptions);

            using (diagnostics.GetOverallScope())
            {
                if (!this.lazyContainerRid.ValueInitialized)
                {
                    using (diagnostics.CreateScope("InitializeContainerResourceId"))
                    {
                        TryCatch <string> tryInitializeContainerRId = await this.lazyContainerRid.GetValueAsync(cancellationToken);

                        if (!tryInitializeContainerRId.Succeeded)
                        {
                            CosmosException cosmosException = tryInitializeContainerRId.Exception.InnerException as CosmosException;
                            return(cosmosException.ToCosmosResponseMessage(new RequestMessage(method: null, requestUriString: null, diagnosticsContext: diagnostics)));
                        }
                    }

                    using (diagnostics.CreateScope("InitializeContinuation"))
                    {
                        if (this.FeedRangeContinuation != null)
                        {
                            TryCatch validateContainer = this.FeedRangeContinuation.ValidateContainer(this.lazyContainerRid.Result.Result);
                            if (!validateContainer.Succeeded)
                            {
                                return(CosmosExceptionFactory.CreateBadRequestException(
                                           message: validateContainer.Exception.InnerException.Message,
                                           innerException: validateContainer.Exception.InnerException,
                                           diagnosticsContext: diagnostics).ToCosmosResponseMessage(new RequestMessage(method: null, requestUriString: null, diagnosticsContext: diagnostics)));
                            }
                        }
                        else
                        {
                            await this.InitializeFeedContinuationAsync(cancellationToken);
                        }
                    }
                }

                return(await this.ReadNextInternalAsync(diagnostics, cancellationToken));
            }
        }
        public override async Task <ResponseMessage> ReadItemStreamAsync(
            string id,
            PartitionKey partitionKey,
            ItemRequestOptions requestOptions   = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContext.Create(requestOptions);

            using (diagnosticsContext.GetOverallScope())
            {
                return(await this.ProcessItemStreamAsync(
                           partitionKey : partitionKey,
                           itemId : id,
                           streamPayload : null,
                           operationType : OperationType.Read,
                           requestOptions : requestOptions,
                           diagnosticsContext : diagnosticsContext,
                           cancellationToken : cancellationToken));
            }
        }
        private Task <TResult> RunWithSynchronizationContextAndDiagnosticsHelperAsync <TResult>(
            string operationName,
            RequestOptions requestOptions,
            Func <CosmosDiagnosticsContext, Task <TResult> > task)
        {
            Debug.Assert(SynchronizationContext.Current != null, "This should only be used when a SynchronizationContext is specified");

            CosmosDiagnosticsContext diagnosticsContext = this.CreateDiagnosticContext(
                operationName,
                requestOptions);

            // Used on NETFX applications with SynchronizationContext when doing locking calls
            return(Task.Run(async() =>
            {
                using (diagnosticsContext.GetOverallScope())
                    using (diagnosticsContext.CreateScope("SynchronizationContext"))
                    {
                        return await task(diagnosticsContext);
                    }
            }));
        }
        public override async Task <ItemResponse <T> > DeleteItemAsync <T>(
            string id,
            PartitionKey partitionKey,
            ItemRequestOptions requestOptions   = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContext.Create(requestOptions);

            using (diagnosticsContext.GetOverallScope())
            {
                Task <ResponseMessage> response = this.ProcessItemStreamAsync(
                    partitionKey: partitionKey,
                    itemId: id,
                    streamPayload: null,
                    operationType: OperationType.Delete,
                    requestOptions: requestOptions,
                    diagnosticsContext: diagnosticsContext,
                    cancellationToken: cancellationToken);

                return(await this.ClientContext.ResponseFactory.CreateItemResponseAsync <T>(response));
            }
        }