public static CosmosClient CreateMockCosmosClient(CosmosRequestHandler preProcessingHandler = null, CosmosConfiguration configuration = null) { DocumentClient documentClient = new MockDocumentClient(); CosmosConfiguration cosmosConfiguration = configuration?.AddCustomHandlers(preProcessingHandler) ?? new CosmosConfiguration("http://localhost", Guid.NewGuid().ToString()) .AddCustomHandlers(preProcessingHandler); return(new CosmosClient( cosmosConfiguration, documentClient)); }
// Async main requires c# 7.1 which is set in the csproj with the LangVersion attribute public static async Task Main(string[] args) { IConfigurationRoot configuration = new ConfigurationBuilder() .AddJsonFile("appSettings.json") .Build(); string endpoint = configuration["EndPointUrl"]; if (string.IsNullOrEmpty(endpoint)) { throw new ArgumentNullException("Please specify a valid endpoint in the appSettings.json"); } string authKey = configuration["AuthorizationKey"]; if (string.IsNullOrEmpty(authKey) || string.Equals(authKey, "Super secret key")) { throw new ArgumentException("Please specify a valid AuthorizationKey in the appSettings.json"); } // Connecting to Emulator. Change if you want a live account CosmosConfiguration cosmosConfiguration = new CosmosConfiguration(endpoint, authKey); cosmosConfiguration.AddCustomHandlers( new LoggingHandler(), new ConcurrencyHandler(), new ThrottlingHandler() ); CosmosClient client = new CosmosClient(cosmosConfiguration); CosmosDatabaseResponse databaseResponse = await client.Databases.CreateDatabaseIfNotExistsAsync("mydb"); CosmosDatabase database = databaseResponse.Database; CosmosContainerResponse containerResponse = await database.Containers.CreateContainerIfNotExistsAsync("mycoll", "/id"); CosmosContainer container = containerResponse.Container; Item item = new Item() { Id = Guid.NewGuid().ToString(), Name = "Test Item", Description = "Some random test item", Completed = false }; // Create await container.Items.CreateItemAsync <Item>(item.Id, item); item.Completed = true; // Replace await container.Items.ReplaceItemAsync <Item>(item.Id, item.Id, item); // Querying CosmosResultSetIterator <Item> query = container.Items.CreateItemQuery <Item>(new CosmosSqlQueryDefinition("SELECT * FROM c"), maxConcurrency: 1); List <Item> results = new List <Item>(); while (query.HasMoreResults) { CosmosQueryResponse <Item> response = await query.FetchNextSetAsync(); results.AddRange(response.ToList()); } // Read Item CosmosItemResponse <Item> cosmosItemResponse = await container.Items.ReadItemAsync <Item>(item.Id, item.Id); AccessCondition accessCondition = new AccessCondition { Condition = cosmosItemResponse.ETag, Type = AccessConditionType.IfMatch }; // Concurrency List <Task <CosmosItemResponse <Item> > > tasks = new List <Task <CosmosItemResponse <Item> > >(); tasks.Add(UpdateItemForConcurrency(container, accessCondition, item)); tasks.Add(UpdateItemForConcurrency(container, accessCondition, item)); try { await Task.WhenAll(tasks); } catch (CosmosException ex) { // Verify that our custom handler catched the scenario Debug.Assert(999.Equals(ex.SubStatusCode)); } // Delete await container.Items.DeleteItemAsync <Item>(item.Id, item.Id); }