예제 #1
0
        private static CosmosClient CreateCosmosClient(string connectionString, CosmosOptions cosmosOptions)
        {
            GuardEmptyString(connectionString, "cosmosDbConnectionString");
            cosmosOptions.Guard();

            var builder = new CosmosClientBuilder(connectionString);

            // ConnectionPolicy のDefault値は、SDKのConnectionPolicy.csで設定されています。

            // ## Connection Mode について
            // Default で ConnectionMode.Direct/Protocol.Tcp で接続されます。
            // もしGateway(ConnectionMode.Gateway/Protocol.Https) を使いたければ、以下メソッドを呼ぶ
            // builder.UseConnectionModeGateway(maxConnectionLimit: ??);

            // Default: CamelCase Serialize/Deserialize and ignore Readonly property
            // TODO: 設定変更用のconfigは未実装
            //var settings = JsonSerializerSettingsFactory.CreateForReadonlyIgnoreAndCamelCase();
            var settings = JsonSerializerSettingsFactory.CreateForCamelCase();

            builder.UseCustomJsonSerializer(new CustomizableCaseJsonSerializer(settings));

            if (cosmosOptions.ThrottlingRetryOptions != null)
            {
                builder.UseThrottlingRetryOptions(cosmosOptions.ThrottlingRetryOptions.MaxRetryWaitTimeOnThrottledRequests,
                                                  cosmosOptions.ThrottlingRetryOptions.MaxRetryAttemptsOnThrottledRequests);
            }

            // multi-master support
            if (!string.IsNullOrEmpty(cosmosOptions.CurrentRegion))
            {
                builder.UseCurrentRegion(cosmosOptions.CurrentRegion);
            }

            return(builder.Build());
        }
        public void VerifyCosmosConfigurationPropertiesGetUpdated()
        {
            string               endpoint          = AccountEndpoint;
            string               key               = Guid.NewGuid().ToString();
            string               region            = CosmosRegions.WestCentralUS;
            ConnectionMode       connectionMode    = ConnectionMode.Gateway;
            TimeSpan             requestTimeout    = TimeSpan.FromDays(1);
            int                  maxConnections    = 9001;
            string               userAgentSuffix   = "testSuffix";
            CosmosRequestHandler preProcessHandler = new TestHandler();
            ApiType              apiType           = ApiType.Sql;
            int                  maxRetryAttemptsOnThrottledRequests = 9999;
            TimeSpan             maxRetryWaitTime = TimeSpan.FromHours(6);

            CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder(
                accountEndPoint: endpoint,
                accountKey: key);

            CosmosClient cosmosClient = cosmosClientBuilder.Build(new MockDocumentClient());
            CosmosClientConfiguration configuration = cosmosClient.Configuration;

            Assert.AreEqual(endpoint, configuration.AccountEndPoint.OriginalString, "AccountEndPoint did not save correctly");
            Assert.AreEqual(key, configuration.AccountKey, "AccountKey did not save correctly");

            //Verify the default values are different from the new values
            Assert.AreNotEqual(region, configuration.CurrentRegion);
            Assert.AreNotEqual(connectionMode, configuration.ConnectionMode);
            Assert.AreNotEqual(maxConnections, configuration.MaxConnectionLimit);
            Assert.AreNotEqual(requestTimeout, configuration.RequestTimeout);
            Assert.AreNotEqual(userAgentSuffix, configuration.UserAgentSuffix);
            Assert.AreNotEqual(apiType, configuration.ApiType);
            Assert.IsNull(configuration.CustomHandlers);

            //Verify GetConnectionPolicy returns the correct values for default
            ConnectionPolicy policy = configuration.GetConnectionPolicy();

            Assert.AreEqual(ConnectionMode.Direct, policy.ConnectionMode);
            Assert.AreEqual(Protocol.Tcp, policy.ConnectionProtocol);
            Assert.AreEqual(configuration.MaxConnectionLimit, policy.MaxConnectionLimit);
            Assert.AreEqual(configuration.RequestTimeout, policy.RequestTimeout);

            cosmosClientBuilder.UseCurrentRegion(region)
            .UseConnectionModeGateway(maxConnections)
            .UseRequestTimeout(requestTimeout)
            .UseUserAgentSuffix(userAgentSuffix)
            .AddCustomHandlers(preProcessHandler)
            .UseApiType(apiType)
            .UseThrottlingRetryOptions(maxRetryWaitTime, maxRetryAttemptsOnThrottledRequests);

            cosmosClient  = cosmosClientBuilder.Build(new MockDocumentClient());
            configuration = cosmosClient.Configuration;

            //Verify all the values are updated
            Assert.AreEqual(region, configuration.CurrentRegion);
            Assert.AreEqual(connectionMode, configuration.ConnectionMode);
            Assert.AreEqual(maxConnections, configuration.MaxConnectionLimit);
            Assert.AreEqual(requestTimeout, configuration.RequestTimeout);
            Assert.AreEqual(userAgentSuffix, configuration.UserAgentSuffix);
            Assert.AreEqual(preProcessHandler, configuration.CustomHandlers[0]);
            Assert.AreEqual(apiType, configuration.ApiType);
            Assert.AreEqual(maxRetryAttemptsOnThrottledRequests, configuration.MaxRetryAttemptsOnThrottledRequests);
            Assert.AreEqual(maxRetryWaitTime, configuration.MaxRetryWaitTimeOnThrottledRequests);

            //Verify GetConnectionPolicy returns the correct values
            policy = configuration.GetConnectionPolicy();
            Assert.AreEqual(region, policy.PreferredLocations[0]);
            Assert.AreEqual(ConnectionMode.Gateway, policy.ConnectionMode);
            Assert.AreEqual(Protocol.Https, policy.ConnectionProtocol);
            Assert.AreEqual(maxConnections, policy.MaxConnectionLimit);
            Assert.AreEqual(requestTimeout, policy.RequestTimeout);
            Assert.AreEqual(userAgentSuffix, policy.UserAgentSuffix);
            Assert.IsTrue(policy.UseMultipleWriteLocations);
            Assert.AreEqual(maxRetryAttemptsOnThrottledRequests, policy.RetryOptions.MaxRetryAttemptsOnThrottledRequests);
            Assert.AreEqual((int)maxRetryWaitTime.TotalSeconds, policy.RetryOptions.MaxRetryWaitTimeInSeconds);
        }