Exemplo n.º 1
0
        public async Task NoContentResponseBulkTest()
        {
            CosmosClientBuilder cosmosClientBuilder = TestCommon.GetDefaultConfiguration();

            cosmosClientBuilder = cosmosClientBuilder.WithBulkExecution(true).WithContentResponseOnWrite(false);
            CosmosClient bulkClient    = cosmosClientBuilder.Build();
            Container    bulkContainer = bulkClient.GetContainer(this.databaseWithoutFlag.Id, this.containerWithoutFlag.Id);

            string pkId = "TestBulkId";
            List <Task <ItemResponse <ToDoActivity> > > bulkOperations = new List <Task <ItemResponse <ToDoActivity> > >();
            List <ToDoActivity> items = new List <ToDoActivity>();

            for (int i = 0; i < 50; i++)
            {
                ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(pk: pkId);
                items.Add(item);
                bulkOperations.Add(bulkContainer.CreateItemAsync <ToDoActivity>(item));
            }

            foreach (Task <ItemResponse <ToDoActivity> > result in bulkOperations)
            {
                ItemResponse <ToDoActivity> itemResponse = await result;
                Assert.AreEqual(itemResponse.StatusCode, HttpStatusCode.Created);
                Assert.IsNull(itemResponse.Resource);
            }
        }
        public static CosmosClient CreateCosmosClient(this ClientContextOptions options)
        {
            options.Validate();

            var clientBuilder = new CosmosClientBuilder(
                options.ConnectionString);

            if (options.UseThrottling)
            {
                clientBuilder = clientBuilder
                                .WithThrottlingRetryOptions(
                    options.MaxRetryWaitTimeOnThrottledRequests ?? new TimeSpan(0, 0, 0, 30),
                    options.MaxRetryAttemptsOnThrottledRequests ?? 3);
            }

            //if (Enum.TryParse<ConsistencyLevel>(options.ConsistencyLevel, out var consistencyLevel))
            //    clientBuilder = clientBuilder
            //        .WithConsistencyLevel(consistencyLevel);

            if (options.AllowBulkExecution.HasValue)
            {
                clientBuilder = clientBuilder
                                .WithBulkExecution(options.AllowBulkExecution.Value);
            }

            if (options.CamelCasePropertyNames)
            {
                clientBuilder = clientBuilder.WithSerializerOptions(new CosmosSerializationOptions
                {
                    PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase
                });
            }

            return(clientBuilder.Build());
        }