Exemple #1
0
        // <InitializeCosmosClientInstanceAsync>
        /// <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, "/id");

            return(cosmosDbService);
        }
        // <InitializeCosmosClientInstanceAsync>
        /// <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;
            CosmosClientBuilder clientBuilder = new CosmosClientBuilder(account, key);
            CosmosClient        client        = clientBuilder
                                                .WithConnectionModeDirect()
                                                .Build();
            CosmosDbService  cosmosDbService = new CosmosDbService(client, databaseName, containerName);
            DatabaseResponse database        = await client.CreateDatabaseIfNotExistsAsync(databaseName);

            await database.Database.CreateContainerIfNotExistsAsync(containerName, "/id");

            return(cosmosDbService);
        }
Exemple #3
0
        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;

            if (account.StartsWith("<"))
            {
                account = File.ReadAllText("C:\\apikeys\\sql-api-dotnet-application\\endpointurl.txt");
            }
            if (key.StartsWith("<"))
            {
                key = File.ReadAllText("C:\\apikeys\\sql-api-dotnet-application\\authorizationkey.txt");
            }
            CosmosClientBuilder clientBuilder   = new CosmosClientBuilder(account, key);
            CosmosClient        client          = clientBuilder.WithConnectionModeDirect().Build();
            CosmosDbService     cosmosDbService = new CosmosDbService(client, databaseName, containerName);
            DatabaseResponse    database        = await client.CreateDatabaseIfNotExistsAsync(databaseName);

            await database.Database.CreateContainerIfNotExistsAsync(containerName, "/id"); return(cosmosDbService);
        }