コード例 #1
0
        public async Task CreateAndInitializeTest()
        {
            int httpCallsMade = 0;
            HttpClientHandlerHelper httpClientHandlerHelper = new HttpClientHandlerHelper
            {
                RequestCallBack = (request, cancellationToken) =>
                {
                    httpCallsMade++;
                    return(null);
                }
            };

            (string endpoint, string authKey) = TestCommon.GetAccountInfo();
            List <(string, string)> containers = new List <(string, string)>
            {
                ("ClientCreateAndInitializeDatabase", "ClientCreateAndInitializeContainer")
            };

            CosmosClientOptions cosmosClientOptions = new CosmosClientOptions
            {
                HttpClientFactory = () => new HttpClient(httpClientHandlerHelper)
            };

            CosmosClient cosmosClient = await CosmosClient.CreateAndInitializeAsync(endpoint, authKey, containers, cosmosClientOptions);

            Assert.IsNotNull(cosmosClient);
            int httpCallsMadeAfterCreation = httpCallsMade;

            ContainerInternal           container    = (ContainerInternal)cosmosClient.GetContainer("ClientCreateAndInitializeDatabase", "ClientCreateAndInitializeContainer");
            ItemResponse <ToDoActivity> readResponse = await container.ReadItemAsync <ToDoActivity>("1", new Cosmos.PartitionKey("Status1"));

            Assert.AreEqual(httpCallsMade, httpCallsMadeAfterCreation);
            cosmosClient.Dispose();
        }
コード例 #2
0
ファイル: CosmosDbService.cs プロジェクト: plzm/always-on
        private async Task Initialize()
        {
            // Cosmos DB client configuration options
            CosmosClientOptions clientOptions = new CosmosClientOptions()
            {
                ApplicationRegion            = this.AzureRegion,
                ConnectionMode               = ConnectionMode.Direct,
                ConsistencyLevel             = ConsistencyLevel.Eventual,
                EnableContentResponseOnWrite = false,
                HttpClientFactory            = (this.HttpClientFactory != null ? this.HttpClientFactory.CreateClient : null)
            };

            // This list is to pre-warm the Cosmos DB client
            List <ValueTuple <string, string> > containers = new List <(string, string)>();

            containers.Add((this.DatabaseName, this.ProfileContainerName));
            containers.Add((this.DatabaseName, this.ProgressContainerName));

            // Get the actual Cosmos DB client
            this.CosmosClient = await CosmosClient.CreateAndInitializeAsync(this.ConnectionString, containers.AsReadOnly(), clientOptions);

            // Container proxies
            this.ProfileContainer  = this.CosmosClient.GetContainer(this.DatabaseName, this.ProfileContainerName);
            this.ProgressContainer = this.CosmosClient.GetContainer(this.DatabaseName, this.ProgressContainerName);

            this.TelemetryClient?.TrackTrace("CosmosDbService.Initialize:Complete", SeverityLevel.Information);
        }
コード例 #3
0
        public async Task AuthIncorrectTest()
        {
            List <(string databaseId, string containerId)> containers = new List <(string databaseId, string containerId)>
            {
                ("ClientCreateAndInitializeDatabase", "ClientCreateAndInitializeContainer")
            };
            string       authKey      = TestCommon.GetAccountInfo().authKey;
            CosmosClient cosmosClient = await CosmosClient.CreateAndInitializeAsync("https://127.0.0.1:0000/", authKey, containers);

            cosmosClient.Dispose();
        }
コード例 #4
0
        public async Task ContainerIncorrectTest()
        {
            List <(string databaseId, string containerId)> containers = new List <(string databaseId, string containerId)>
            {
                ("ClientCreateAndInitializeDatabase", "IncorrectContainer")
            };

            (string endpoint, string authKey) = TestCommon.GetAccountInfo();
            try
            {
                CosmosClient cosmosClient = await CosmosClient.CreateAndInitializeAsync(endpoint, authKey, containers);
            }
            catch (CosmosException ex)
            {
                Assert.IsTrue(ex.StatusCode == HttpStatusCode.NotFound);
                throw ex;
            }
        }