public static CosmosClient CreateCosmosClient(this ClientContextOptions options) { options.Validate(); var clientBuilder = new CosmosClientBuilder( options.ConnectionString); if (options.UseThrottling) { clientBuilder = clientBuilder .WithThrottlingRetryOptions( options.MaxRetryWaitTimeOnThrottledRequests ?? new TimeSpan(0, 0, 0, 30), options.MaxRetryAttemptsOnThrottledRequests ?? 3); } //if (Enum.TryParse<ConsistencyLevel>(options.ConsistencyLevel, out var consistencyLevel)) // clientBuilder = clientBuilder // .WithConsistencyLevel(consistencyLevel); if (options.AllowBulkExecution.HasValue) { clientBuilder = clientBuilder .WithBulkExecution(options.AllowBulkExecution.Value); } if (options.CamelCasePropertyNames) { clientBuilder = clientBuilder.WithSerializerOptions(new CosmosSerializationOptions { PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase }); } return(clientBuilder.Build()); }
private void InitializeClient() { if (ConnectionString.IsNullOrEmpty()) { ConnectionString = BuildStorageConnection().GetAwaiter().GetResult(); } var clientBuilder = new CosmosClientBuilder(ConnectionString); clientBuilder.WithThrottlingRetryOptions(TimeSpan.FromMilliseconds(500), 3); var client = clientBuilder .WithConnectionModeDirect() .Build(); _cloudClient = client ?? throw new InvalidOperationException("Cannot build Cosmos Client using connection string"); // Create the database if it does not exist and been instructed to. if (_createIfNotExists) { var throughputProperties = _autoscaleDatabaseThroughput ? ThroughputProperties.CreateAutoscaleThroughput(_maxThroughput) : null; _cloudClient.CreateDatabaseIfNotExistsAsync(DatabaseName, throughputProperties).GetAwaiter().GetResult(); } // Create any tables required up front. if (!_createTableNames.IsNullOrDefault()) { foreach (var tableName in _createTableNames) { CreateTable(tableName).GetAwaiter().GetResult(); } } }
private static Lazy <CosmosClient> SetupClient(ICosmosContainerOptions options) { var csb = ValidatInput(options.ConnectionString, options.DatabaseId, options.ContainerId); var cacheKey = $"{options.DatabaseId}_{options.UseGatewayMode}_{ToSHA256(csb.Key)}"; return(new Lazy <CosmosClient>(() => { CosmosClient client = null; bool fromCache = false; if (_clientCache.ContainsKey(cacheKey)) { client = _clientCache[cacheKey]; if (client != null) { fromCache = true; } } if (!fromCache) { var builder = new CosmosClientBuilder(options.ConnectionString); builder .WithThrottlingRetryOptions(TimeSpan.FromSeconds(30), 10); if (options.UseGatewayMode) { builder.WithConnectionModeGateway(options.ConcurrentConnections); } foreach (var region in options.AzureRegions ?? new List <string>()) { builder.WithApplicationRegion(region); } client = builder.Build(); _clientCache[cacheKey] = client; } CreateDatabaseIfNotExistsAsync(client, options).GetAwaiter().GetResult(); return client; })); }
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.WithCustomSerializer(new CustomizableCaseJsonSerializer(settings)); if (cosmosOptions.ThrottlingRetryOptions != null) { builder.WithThrottlingRetryOptions( cosmosOptions.ThrottlingRetryOptions.MaxRetryWaitTimeOnThrottledRequests, cosmosOptions.ThrottlingRetryOptions.MaxRetryAttemptsOnThrottledRequests); } // multi-master support if (!string.IsNullOrEmpty(cosmosOptions.CurrentRegion)) { builder.WithApplicationRegion(cosmosOptions.CurrentRegion); } return(builder.Build()); }