/// <summary> /// Initialises Cosmos DB client connection and container. /// </summary> /// <param name="configurationSection"></param> /// <returns></returns> private static async Task <CosmosDBService> InitialiseCosmosClientInstanceAsync(IConfigurationSection configurationSection) { // Set connection parameters string databaseName = configurationSection.GetSection("DatabaseName").Value; string containerName = configurationSection.GetSection("ContainerName").Value; string account = configurationSection.GetSection("Account").Value; string key = configurationSection.GetSection("Key").Value; // Build client and services Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder clientBuilder = new Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder(account, key); Microsoft.Azure.Cosmos.CosmosClient client = clientBuilder.WithConnectionModeDirect().Build(); CosmosDBService dBService = new CosmosDBService(client, databaseName, containerName); // Initialise database, if required Microsoft.Azure.Cosmos.DatabaseResponse database = await client.CreateDatabaseIfNotExistsAsync(databaseName); // Build the database container await database.Database.CreateContainerIfNotExistsAsync(containerName, "/id"); return(dBService); }
public async Task <HttpStatusCode> CreateUser(ClaimsPrincipal user, string deskId) { UserInfo userInfoObj = new UserInfo() { email = user.FindFirst("emails").Value, firstName = user.FindFirst(ClaimTypes.GivenName).Value, lastName = user.FindFirst(ClaimTypes.Surname).Value, id = user.FindFirst(ClaimTypes.NameIdentifier).Value, deskId = deskId }; //returnMessage = JsonConvert.SerializeObject(userInfoObj); Microsoft.Azure.Cosmos.CosmosClient client = new Microsoft.Azure.Cosmos.CosmosClient(_cosmosDBOptions.DatabaseEndpoint, _cosmosDBOptions.SecretKey); Microsoft.Azure.Cosmos.Database database = await client.CreateDatabaseIfNotExistsAsync(_cosmosDBOptions.DatabaseName); Microsoft.Azure.Cosmos.Container container = await database.CreateContainerIfNotExistsAsync( "users", "/email", 400); // Upsert an item Microsoft.Azure.Cosmos.ItemResponse <UserInfo> createResponse = await container.UpsertItemAsync(userInfoObj); return(createResponse.StatusCode); }
private static async Task <CosmosDbService <ClientData> > InitializeCosmosClientInstanceClientDataAsync(IConfigurationSection configurationSection) { string databaseName = configurationSection.GetSection("DatabaseName").Value; string containerName = configurationSection.GetSection("ClientsContainerName").Value; string account = configurationSection.GetSection("Account").Value; string key = configurationSection.GetSection("Key").Value; Microsoft.Azure.Cosmos.CosmosClient client = new Microsoft.Azure.Cosmos.CosmosClient(account, key); CosmosDbService <ClientData> cosmosDbService = new CosmosDbService <ClientData>(client, databaseName, containerName); Microsoft.Azure.Cosmos.DatabaseResponse database = await client.CreateDatabaseIfNotExistsAsync(databaseName); await database.Database.CreateContainerIfNotExistsAsync(containerName, "/id"); //Upsert client seeding data if (await cosmosDbService.GetItemAsync(CreateBcnOfficeClient().Id) == null) { await cosmosDbService.AddItemAsync(CreateBcnOfficeClient()); } else { // await cosmosDbService.UpdateItemAsync(CreateBcnOfficeClient().Id, CreateBcnOfficeClient()); } return(cosmosDbService); }
public static CosmosDbClient Create(CosmosDbOptions cosmosDbOptions) { Microsoft.Azure.Cosmos.CosmosClient cosmosClient = new Microsoft.Azure.Cosmos.CosmosClient(cosmosDbOptions.Account, cosmosDbOptions.Key); var cosmosDbClient = new CosmosDbClient(cosmosClient, cosmosDbOptions.DatabaseName, cosmosDbOptions.ContainerName); //Microsoft.Azure.Cosmos.DatabaseResponse database = cosmosClient.CreateDatabaseIfNotExistsAsync(cosmosDbOptions.DatabaseName).Result; //database.Database.CreateContainerIfNotExistsAsync(cosmosDbOptions.ContainerName, "/id").Wait(); return(cosmosDbClient); }
private static CosmosDbService InitializeCosmosClientInstance(CosmosConfig cosmosConfig) { Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder clientBuilder = new Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder(cosmosConfig.ConnectionString); Microsoft.Azure.Cosmos.CosmosClient client = clientBuilder .WithConnectionModeDirect() .Build(); CosmosDbService cosmosDbService = new CosmosDbService(client, cosmosConfig.DatabaseName, cosmosConfig.ContainerName); return(cosmosDbService); }
private static InterUserMessageRepository InitializeCosmosClientInstance(InterUserMessageConfig interUserMessageConfig) { Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder clientBuilder = new Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder(interUserMessageConfig.ConnectionString); Microsoft.Azure.Cosmos.CosmosClient client = clientBuilder .WithConnectionModeDirect() .Build(); InterUserMessageRepository interUserMessageRepository = new InterUserMessageRepository(client, interUserMessageConfig.DatabaseName, interUserMessageConfig.ContainerName); return(interUserMessageRepository); }
private static LinkRepository InitializeCosmosClientInstance(LinkConfig linkConfig) { Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder clientBuilder = new Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder(linkConfig.ConnectionString); Microsoft.Azure.Cosmos.CosmosClient client = clientBuilder .WithConnectionModeDirect() .Build(); LinkRepository linkRepository = new LinkRepository(client, linkConfig.DatabaseName, linkConfig.ContainerName); return(linkRepository); }
private static CosmosDbService InitializeCosmosClientInstanceAsync() { string databaseName = "doorbell"; string containerName = "iot"; string account = "https://statuslight.documents.azure.com:443/"; string key = "2fZIOAwWv200yehiFmu0jhHTNQpz9fdWrwOd8EoKoRqrVJQNiO1QZbSlTitfd1iPSpp7xRl8Kj75mzdNDd6eqw=="; Microsoft.Azure.Cosmos.CosmosClient client = new Microsoft.Azure.Cosmos.CosmosClient(account, key); CosmosDbService cosmosDbService = new CosmosDbService(client, databaseName, containerName); return(cosmosDbService); }
private static UserData InitializeUserDBClientInstanceAsync(IConfigurationSection configurationSection) { string databaseName = configurationSection.GetSection("DatabaseName").Value; string containerName = configurationSection.GetSection("ContainerName").Value; string account = configurationSection.GetSection("Account").Value; string key = configurationSection.GetSection("Key").Value; Microsoft.Azure.Cosmos.CosmosClient client = new Microsoft.Azure.Cosmos.CosmosClient(account, key); UserData userData = new UserData(client, databaseName, containerName); return(userData); }
private static CosmosDbService InitializeCosmosClientInstance(CosmosConfig cosmosConfig) { Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder clientBuilder = new Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder(cosmosConfig.ConnectionString); Microsoft.Azure.Cosmos.CosmosClient client = clientBuilder .WithConnectionModeDirect() .Build(); CosmosDbService cosmosDbService = new CosmosDbService(client, cosmosConfig.DatabaseName, cosmosConfig.ContainerName); //Microsoft.Azure.Cosmos.DatabaseResponse database = await client.CreateDatabaseIfNotExistsAsync(databaseName); //await database.Database.CreateContainerIfNotExistsAsync(containerName, "/id"); return(cosmosDbService); }
/// <summary> /// Creates a Cosmos DB database and a container with the specified partition key. /// </summary> /// <returns></returns> private async Task <CosmosDbService <T> > InitializeCosmosClientInstanceAsync <T>() { Microsoft.Azure.Cosmos.CosmosClient client = new Microsoft.Azure.Cosmos.CosmosClient(Configuration.GetValue <string>("ConnectionStrings:Default", null)); var databaseName = "Fagkveld"; var containerName = "ViktigeData"; var cosmosDbService = new CosmosDbService <T>(client, databaseName, containerName); Microsoft.Azure.Cosmos.DatabaseResponse database = await client.CreateDatabaseIfNotExistsAsync(databaseName); await database.Database.CreateContainerIfNotExistsAsync(containerName, "/_partitionKey"); return(cosmosDbService); }
/// <summary> /// Register a singleton instance of Cosmos Db Container Factory, which is a wrapper for the CosmosClient. /// </summary> /// <param name="services"></param> /// <param name="endpointUrl"></param> /// <param name="primaryKey"></param> /// <param name="databaseName"></param> /// <param name="containers"></param> /// <returns></returns> public static IServiceCollection AddCosmosDb(this IServiceCollection services, string endpointUrl, string primaryKey, string databaseName, List <ContainerInfo> containers) { Microsoft.Azure.Cosmos.CosmosClient client = new Microsoft.Azure.Cosmos.CosmosClient(endpointUrl, primaryKey); var cosmosDbClientFactory = new CosmosDbContainerFactory(client, databaseName, containers); services.AddSingleton <ICosmosDbContainerFactory>(cosmosDbClientFactory); return(services); }
/// <summary> /// Default constructor /// </summary> /// <param name="endpoint">Azure Cosmos DB endpoint</param> /// <param name="key">Azure Cosmos DB authentication key</param> /// <param name="databaseId">Azure Cosmos DB database ID</param> /// <param name="collectionId">Azure Cosmos DB collection ID</param> /// <param name="partitionKeyValue">Partition key value. Only the items matching this value will be visible.</param> public CosmosDbRepository(Uri endpoint, string key, string databaseId, string collectionId, string partitionKeyValue) { client = new DocumentClient(endpoint, key); cosmosClient = new Microsoft.Azure.Cosmos.CosmosClient(endpoint.ToString(), key); this.databaseId = databaseId; this.collectionId = collectionId; databaseUri = UriFactory.CreateDatabaseUri(databaseId); documentCollectionUri = UriFactory.CreateDocumentCollectionUri(databaseId, this.collectionId); documentUriFactory = documentId => UriFactory.CreateDocumentUri(databaseId, this.collectionId, documentId); this.partitionKeyValue = partitionKeyValue; partitionKeyObjectForRequestOptions = new PartitionKey(this.partitionKeyValue); CreateDatabaseIfNotExistsAsync().Wait(); CreateCollectionIfNotExistsAsync().Wait(); }
// Creates a Cosmos DB database and a container with the specified partition key. private static async Task <PatientService> InitializeCosmosClientInstanceAsync(IConfigurationSection configurationSection) { var databaseName = configurationSection.GetSection("DatabaseName").Value; var containerName = configurationSection.GetSection("ContainerName").Value; var account = configurationSection.GetSection("Account").Value; var key = configurationSection.GetSection("Key").Value; var client = new Microsoft.Azure.Cosmos.CosmosClient(account, key); var patientService = new PatientService(client, databaseName, containerName); var database = await client.CreateDatabaseIfNotExistsAsync(databaseName); await database.Database.CreateContainerIfNotExistsAsync(containerName, "/id"); return(patientService); }
private ConversationReferenceDao InitializeCosmosClientInstance() { string account = configuration["CosmosDbEndpoint"]; string key = configuration["CosmosDbAuthKey"]; Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder clientBuilder = new Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder(account, key); Microsoft.Azure.Cosmos.CosmosClient client = clientBuilder .WithConnectionModeDirect() .Build(); ConversationReferenceDao cosmosDbService = new ConversationReferenceDao(client, configuration); return(cosmosDbService); }
/// <summary> /// Creates a Cosmos DB database and a container with the specified partition key. /// </summary> /// <returns></returns> private static async Task <CosmosDbService> InitializeCosmosClientInstanceAsync(IConfigurationSection configurationSection) { string databaseName = configurationSection.GetSection("DatabaseName").Value; string containerName = configurationSection.GetSection("ContainerName").Value; string account = configurationSection.GetSection("Account").Value; string key = configurationSection.GetSection("Key").Value; Microsoft.Azure.Cosmos.CosmosClient client = new Microsoft.Azure.Cosmos.CosmosClient(account, key); CosmosDbService cosmosDbService = new CosmosDbService(client, databaseName, containerName); //Microsoft.Azure.Cosmos.DatabaseResponse database = await client.CreateDatabaseIfNotExistsAsync(databaseName); //await database.Database.CreateContainerIfNotExistsAsync(containerName, "/prosaweb"); return(cosmosDbService); }
/// <summary> /// Register a singleton instance of Cosmos Db Container Factory, which is a wrapper for the CosmosClient. /// </summary> /// <param name="services"></param> /// <param name="endpointUrl"></param> /// <param name="primaryKey"></param> /// <param name="databaseName"></param> /// <param name="containers"></param> /// <returns></returns> public static IServiceCollection AddCosmosDb(this IServiceCollection services, string endpointUrl, string primaryKey, string databaseName, List <ContainerInfo> containers) { Microsoft.Azure.Cosmos.CosmosClient client = new Microsoft.Azure.Cosmos.CosmosClient(endpointUrl, primaryKey); CosmosDbContainerFactory cosmosDbClientFactory = new CosmosDbContainerFactory(client, databaseName, containers); // Microsoft recommends a singleton client instance to be used throughout the application // https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.cosmos.cosmosclient?view=azure-dotnet#definition // "CosmosClient is thread-safe. Its recommended to maintain a single instance of CosmosClient per lifetime of the application which enables efficient connection management and performance" services.AddSingleton <ICosmosDbContainerFactory>(cosmosDbClientFactory); return(services); }
// ReSharper disable once UnusedMember.Local private static async Task <ItemsRepoCosmos> InitializeCosmosClientInstanceAsync(CosmosDbOptions cosmosDbOptions) { var client = new Microsoft.Azure.Cosmos.CosmosClient( cosmosDbOptions.Account, cosmosDbOptions.Key); var cosmosDbService = new ItemsRepoCosmos( client, cosmosDbOptions.DatabaseName, cosmosDbOptions.ContainerName); var database = await client.CreateDatabaseIfNotExistsAsync( cosmosDbOptions.DatabaseName); await database.Database.CreateContainerIfNotExistsAsync( cosmosDbOptions.ContainerName, "/id"); return(cosmosDbService); }
public static async Task <UserCosmosDbRepository> InitializeCosmosClientInstanceAsync(IConfigurationSection configurationSection) { string databaseName = configurationSection.GetSection("Radiometry").Value; string containerName = configurationSection.GetSection("Users").Value; string account = configurationSection.GetSection("Account").Value; string key = configurationSection.GetSection("Key").Value; Microsoft.Azure.Cosmos.CosmosClient client = new Microsoft.Azure.Cosmos.CosmosClient(account, key); UserCosmosDbRepository cosmosDbService = new UserCosmosDbRepository(client, databaseName, containerName); Microsoft.Azure.Cosmos.DatabaseResponse database = await client.CreateDatabaseIfNotExistsAsync(databaseName); await database.Database.CreateContainerIfNotExistsAsync(containerName, "/id"); return(cosmosDbService); }
/// <summary> /// Register a singleton instance of Cosmos Db Container Factory, which is a wrapper for the CosmosClient. /// </summary> /// <param name="services"></param> /// <param name="endpointUrl"></param> /// <param name="primaryKey"></param> /// <param name="databaseName"></param> /// <param name="containers"></param> /// <returns></returns> public static IServiceCollection AddCosmosDb(this IServiceCollection services, string endpointUrl, string primaryKey, string databaseName, List <ContainerInfo> containers) { Microsoft.Azure.Cosmos.CosmosClient client = new Microsoft.Azure.Cosmos.CosmosClient(endpointUrl, primaryKey); var cosmosDbClientFactory = new CosmosDbContainerFactory(client, databaseName, containers); // This will be done at the API level so we ONLY ensure db creation in development environment // cosmosDbClientFactory.EnsureDbSetupAsync().Wait(); services.AddSingleton <ICosmosDbContainerFactory>(cosmosDbClientFactory); return(services); }
/// <summary> /// Creates a Cosmos DB database and a container with the specified partition key. /// </summary> /// <returns></returns> private static async Task <CosmosDbService> InitializeCosmosClientInstanceAsync(IConfigurationSection configurationSection) { string databaseName = Environment.GetEnvironmentVariable("DATABASE_NAME"); // configurationSection.GetSection("DatabaseName").Value; string containerName = Environment.GetEnvironmentVariable("CONTAINER_NAME"); // configurationSection.GetSection("ContainerName").Value; string account = Environment.GetEnvironmentVariable("ACCOUNT"); // configurationSection.GetSection("Account").Value; string key = Environment.GetEnvironmentVariable("COSMOS_DB_KEY"); // configurationSection.GetSection("Key").Value; Microsoft.Azure.Cosmos.CosmosClient client = new Microsoft.Azure.Cosmos.CosmosClient(account, key); CosmosDbService cosmosDbService = new CosmosDbService(client, databaseName, containerName); Microsoft.Azure.Cosmos.DatabaseResponse database = await client.CreateDatabaseIfNotExistsAsync(databaseName); await database.Database.CreateContainerIfNotExistsAsync(containerName, "/id"); return(cosmosDbService); }
/// Creates a Cosmos DB database and a container with the specified partition key. private static CosmosDBService InitializeCosmosClientInstanceAsync(IConfigurationSection configurationSection) { string databaseName = configurationSection.GetSection("DatabaseName").Value; string containerName = configurationSection.GetSection("ContainerName").Value; string account = configurationSection.GetSection("Account").Value; string key = configurationSection.GetSection("Key").Value; Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder clientBuilder = new Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder(account, key); Microsoft.Azure.Cosmos.CosmosClient client = clientBuilder .WithConnectionModeDirect() .Build(); CosmosDBService cosmosDbService = new CosmosDBService(client, databaseName); client.CreateDatabaseIfNotExistsAsync(databaseName); return(cosmosDbService); }
/// <summary> /// Creates a Cosmos DB database and a container with the specified partition key. /// </summary> /// <returns></returns> private static async Task <CosmosDbService> InitializeCosmosClientInstanceAsync(string url, string key) { string databaseName = Environment.GetEnvironmentVariable("CosmosDbName"); string containerName = Environment.GetEnvironmentVariable("CosmosDbContainerName"); Microsoft.Azure.Cosmos.CosmosClient client = new Microsoft.Azure.Cosmos.CosmosClient(url, key); CosmosDbService cosmosDbService = new CosmosDbService(client, databaseName, containerName); Microsoft.Azure.Cosmos.DatabaseResponse database = await client.CreateDatabaseIfNotExistsAsync(databaseName); await database.Database.CreateContainerIfNotExistsAsync(containerName, "/id"); await database.Database.CreateContainerIfNotExistsAsync("Identity", "/id"); return(cosmosDbService); }
public static async Task <PersonService> InitializeCosmosClientInstanceAsync(IConfigurationSection configurationSection) { string dbName = configurationSection.GetSection("DatabaseName").Value; string containerName = configurationSection.GetSection("ContainerName").Value; string accountName = configurationSection.GetSection("Account").Value; string key = configurationSection.GetSection("Key").Value; Microsoft.Azure.Cosmos.CosmosClient cosmosClient = new Microsoft.Azure.Cosmos.CosmosClient(accountName, key); PersonService personService = new PersonService(cosmosClient, dbName, containerName); Microsoft.Azure.Cosmos.DatabaseResponse databaseResponse = await cosmosClient.CreateDatabaseIfNotExistsAsync(dbName); await databaseResponse.Database.CreateContainerIfNotExistsAsync(containerName, "/cprnumber"); return(personService); }
private static async Task <ProductsDBServices> InitializeCosmosProductDBAsync(IConfigurationSection configurationSection) { string databaseName = configurationSection.GetSection("DatabaseName").Value; string containerName = configurationSection.GetSection("ContainerName").Value; string account = configurationSection.GetSection("Account").Value; string key = configurationSection.GetSection("Key").Value; Microsoft.Azure.Cosmos.CosmosClient client = new Microsoft.Azure.Cosmos.CosmosClient(account, key); ProductsDBServices productsDBServices = new ProductsDBServices(client, databaseName, containerName); Microsoft.Azure.Cosmos.DatabaseResponse database = await client.CreateDatabaseIfNotExistsAsync(databaseName); await database.Database.CreateContainerIfNotExistsAsync(containerName, "/type"); return(productsDBServices); }
/// <summary> /// Creates a Cosmos DB database and a container with the specified partition key. /// </summary> /// <returns></returns> private static async Task <CosmosDbService> InitializeCosmosClientInstanceAsync(IConfigurationSection configurationSection) { string databaseName = configurationSection.GetSection("DatabaseName").Value; string containerName = configurationSection.GetSection("ContainerName").Value; string account = configurationSection.GetSection("Account").Value; string key = ConfigurationManager.AppSettings["key"]; Microsoft.Azure.Cosmos.CosmosClient client = new Microsoft.Azure.Cosmos.CosmosClient(account, key, new Microsoft.Azure.Cosmos.CosmosClientOptions { ConnectionMode = Microsoft.Azure.Cosmos.ConnectionMode.Gateway }); CosmosDbService cosmosDbService = new CosmosDbService(client, databaseName, containerName); Microsoft.Azure.Cosmos.DatabaseResponse database = await client.CreateDatabaseIfNotExistsAsync(databaseName); await database.Database.CreateContainerIfNotExistsAsync(containerName, "/id"); return(cosmosDbService); }
/// <summary> /// Creates a Cosmos DB database and a container with the specified partition key. /// </summary> /// <returns></returns> private static async Task <ICosmosDbService> InitializeCosmosClientInstanceAsync() { Microsoft.Azure.Cosmos.CosmosClient client = new Microsoft.Azure.Cosmos.CosmosClient(account, key); CosmosDbService cosmosDbService = new CosmosDbService(client, databaseName, containerName); AuditStatus a = new AuditStatus { id = "0C805497-2BA1-4524-9D41-E5D556488F9D", BatchId = "C8F9F1C1-4BD1-447F-9F0E-D8E96DF8A1E7", Status = "start", TimeStamp = DateTime.Now }; Microsoft.Azure.Cosmos.DatabaseResponse database = await client.CreateDatabaseIfNotExistsAsync(databaseName); await database.Database.CreateContainerIfNotExistsAsync(containerName, "/id"); await cosmosDbService.AddAuditStatusAsync(a); return(cosmosDbService); }
private static async Task <CosmosDbImpl> InitializeCosmosClientInstanceAsync(IConfigurationSection configurationSection) { string databaseName = configurationSection.GetSection("DatabaseName").Value; string containerName = configurationSection.GetSection("ContainerName").Value; string account = configurationSection.GetSection("Account").Value; string key = configurationSection.GetSection("Key").Value; Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder clientBuilder = new Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder(account, key); Microsoft.Azure.Cosmos.CosmosClient client = clientBuilder .WithConnectionModeDirect() .Build(); CosmosDbImpl cosmosDbService = new CosmosDbImpl(client, databaseName, containerName); Microsoft.Azure.Cosmos.DatabaseResponse database = await client.CreateDatabaseIfNotExistsAsync(databaseName); await database.Database.CreateContainerIfNotExistsAsync(containerName, "/legendname"); return(cosmosDbService); }
/// <summary> /// Creates a Cosmos DB database and a container with the specified partition key. /// </summary> /// <returns></returns> private static async Task <CosmosDbService> InitializeCosmosClientInstanceAsync(IConfigurationSection configurationSection) { string databaseName = configurationSection.GetSection("DatabaseName").Value; string containerName = configurationSection.GetSection("ContainerName").Value; string account = configurationSection.GetSection("Account").Value; string key = configurationSection.GetSection("Key").Value; Microsoft.Azure.Cosmos.CosmosClient client = new Microsoft.Azure.Cosmos.CosmosClient(account, key); CosmosDbService cosmosDbService = new CosmosDbService(client, databaseName, containerName); Microsoft.Azure.Cosmos.DatabaseResponse database = await client.CreateDatabaseIfNotExistsAsync(databaseName); await database.Database.DefineContainer(name : containerName, partitionKeyPath : "/id") .WithUniqueKey() .Path("/emailAddress") .Attach() .CreateIfNotExistsAsync(); return(cosmosDbService); }
private static async Task <CosmosDBService> InitializeCosmosClientInstanceAsync(IConfiguration Configuration) { // CosmosDB connection information string account = Configuration["CosmosDB:Account"]; string key = Configuration["CosmosDB:Key"]; // CosmosDB database string databaseName = Configuration["CosmosDB:DatabaseName"]; // CosmosDB containers var containerNames = Configuration .AsEnumerable() .Where(o => o.Key.Contains("CosmosDB:ContainerName:")) .Select(o => o.Value) .ToList(); // initialize CosmosDB client Microsoft.Azure.Cosmos.CosmosClient client = new Microsoft.Azure.Cosmos.CosmosClient(account, key); // create database if missing Microsoft.Azure.Cosmos.DatabaseResponse database = await client.CreateDatabaseIfNotExistsAsync(databaseName); // initialize service CosmosDBService cosmosDbService = new CosmosDBService(client); // for each container in list: containerNames.ForEach(async containerName => { // create container inside database, if missing await database.Database.CreateContainerIfNotExistsAsync(containerName, "/id"); // add container to the list inside service await cosmosDbService.AddContainerDefinition(databaseName, containerName); }); // Do nothing. Satisfy debugger's requirement to have "await" in it //await Task.Run(() => {}); return(cosmosDbService); }