public async Task BatchRateLimitingAsync() { Container containerWithDefaultRetryPolicy = BatchTestBase.LowThroughputJsonContainer; await this.CreateJsonTestDocsAsync(containerWithDefaultRetryPolicy); CosmosClient clientWithNoThrottleRetry = new CosmosClientBuilder( BatchTestBase.Client.Endpoint.ToString(), BatchTestBase.Client.AccountKey) .WithThrottlingRetryOptions( maxRetryWaitTimeOnThrottledRequests: default(TimeSpan), maxRetryAttemptsOnThrottledRequests: 0) .Build(); Container containerWithNoThrottleRetry = clientWithNoThrottleRetry.GetContainer(BatchTestBase.Database.Id, BatchTestBase.LowThroughputJsonContainer.Id); // The second batch started should be rate limited by the backend in admission control. { TransactionalBatchResponse[] batchResponses = await this.RunTwoLargeBatchesAsync(containerWithNoThrottleRetry); Assert.AreEqual(HttpStatusCode.OK, batchResponses[0].StatusCode); Assert.AreEqual((int)StatusCodes.TooManyRequests, (int)batchResponses[1].StatusCode); Assert.AreEqual(3200, (int)batchResponses[1].SubStatusCode); } // The default retry policy around throttling should ensure the second batch also succeeds. { TransactionalBatchResponse[] batchResponses = await this.RunTwoLargeBatchesAsync(containerWithDefaultRetryPolicy); Assert.AreEqual(HttpStatusCode.OK, batchResponses[0].StatusCode); Assert.AreEqual(HttpStatusCode.OK, batchResponses[1].StatusCode); } }
static async Task Main(string[] args) { //setup secrets/builder stuff var builder = new ConfigurationBuilder(); builder.AddUserSecrets <Program>(); var Configuration = builder.Build(); var ConnectionString = Configuration["CosmosConnection"]; //setup logger var fac = LoggerFactory.Create(builder => { builder .AddFilter("Microsoft", LogLevel.Warning) .AddFilter("System", LogLevel.Warning) .AddFilter("LoggingConsoleApp.Program", LogLevel.Debug) .AddConsole(); }); _logger = fac.CreateLogger <Program>(); //startup _logger.LogCritical("Beginning"); Thread.Sleep(2000); _logger.LogInformation("---- Setup DB connection ----"); var client = new CosmosClientBuilder(ConnectionString).WithBulkExecution(true).Build(); var container = client.GetContainer("champlin", "champlin"); _logger.LogInformation("---- Reading file ----"); List <CrewDto> crewList = new List <CrewDto>(); using (FileStream fs = File.OpenRead("crewList.json")) { crewList = await JsonSerializer.DeserializeAsync <List <CrewDto> >(fs); } _logger.LogInformation($"Fetched {crewList.Count} items"); _logger.LogInformation("---- Upserting items now ----"); List <Task> taskList = new List <Task>(); foreach (var crew in crewList) { taskList.Add(container.UpsertItemAsync <CrewDto>(crew, new PartitionKey("UssChamplin:Crew"))); } await Task.WhenAll(taskList); _logger.LogInformation("---- Finished ----"); }
public override void Configure(IFunctionsHostBuilder builder) { builder.Services.AddSingleton(_ => { var cosmosClient = new CosmosClientBuilder(Environment.GetEnvironmentVariable("CosmosDBConnection")) .WithSerializerOptions(new CosmosSerializationOptions { PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase }) .Build(); return(cosmosClient.GetContainer(CosmosDbConstants.DatabaseName, CosmosDbConstants.MonthlyRecordContainerName)); }); }
public CosmosDbService(Settings settings) { var db = settings.CosmosDbSettings.Database; var client = new CosmosClientBuilder(settings.CosmosDbSettings.Connection) .WithSerializerOptions(new CosmosSerializationOptions { PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase }) .Build(); var database = client.CreateDatabaseIfNotExistsAsync(db).Result; database.Database.CreateContainerIfNotExistsAsync(TRANSACTION_CONTAINER, "/location").Wait(); _transactionContainer = client.GetContainer(db, TRANSACTION_CONTAINER); }
static async Task WithCustomSerializerAsync(IConfiguration configuration) { CosmosClient client = new CosmosClientBuilder(configuration.GetConnectionString("Cosmos")) .WithApplicationName("OnDotNetRocks") .WithCustomSerializer(new TextJsonSerializer()) .Build(); ModelTextJson model = new ModelTextJson() { TheIdentifier = Guid.NewGuid().ToString(), DescriptiveTitle = "With custom System.Text.Json serializer!" }; Container container = client.GetContainer("OnDotNet", "episode1serializer"); ItemResponse<ModelTextJson> createdItem = await container.CreateItemAsync(model); Console.WriteLine($"Used custom serializer to create item {createdItem.Resource.TheIdentifier}"); }
public async void CreateLecturer(CreateLecturerDto createLecturerDto) { var connectionString = _configuration.GetConnectionString("CosmosDB"); var client = new CosmosClientBuilder(connectionString) .WithSerializerOptions(new CosmosSerializationOptions { PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase }) .Build(); var createLecturerContainer = client.GetContainer("TestDatabase", "Lecturer"); var lecturer = new CreateLecturerDto { Name = createLecturerDto.Name }; await createLecturerContainer.CreateItemAsync(lecturer); }
static async Task WithSerializerOptionsAsync(IConfiguration configuration) { CosmosClient client = new CosmosClientBuilder(configuration.GetConnectionString("Cosmos")) .WithApplicationName("OnDotNetRocks") .WithSerializerOptions(new CosmosSerializationOptions(){ IgnoreNullValues = true, PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase }) .Build(); ModelJsonNet model = new ModelJsonNet() { Id = Guid.NewGuid().ToString(), DescriptiveTitle = "With customized JSON.Net!" }; Container container = client.GetContainer("OnDotNet", "episode1serializer"); ItemResponse<ModelJsonNet> createdItem = await container.CreateItemAsync(model); Console.WriteLine($"Used serializer options to create item {createdItem.Resource.Id}"); }