public CosmosContainerProvider(ICosmosContainerOptions options) { _options = options ?? throw new ArgumentNullException(nameof(options)); _client = SetupClient(_options); }
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 void ValidateOptions(ICosmosContainerOptions options) { if (string.IsNullOrEmpty(options.ConnectionString)) { throw new ArgumentNullException(nameof(options.ConnectionString)); } if (string.IsNullOrEmpty(options.ContainerId)) { throw new ArgumentNullException(nameof(options.ContainerId)); } if (string.IsNullOrEmpty(options.DatabaseId)) { throw new ArgumentNullException(nameof(options.DatabaseId)); } if (options.DefaultTTLInSeconds.HasValue && options.DefaultTTLInSeconds < 1) { throw new NotSupportedException($"'DefaultTTLInSeconds' cannot be set lower than 1 second."); } if (options.RequestUnits < 400) { options.RequestUnits = 400; } }
public static async Task CreateDatabaseIfNotExistsAsync(CosmosClient client, ICosmosContainerOptions options) { DatabaseResponse databaseCreationResponse; if (options.ProvisionOnDatabaseLevel) { databaseCreationResponse = await client.CreateDatabaseIfNotExistsAsync(options.DatabaseId, options.RequestUnits); } else { databaseCreationResponse = await client.CreateDatabaseIfNotExistsAsync(options.DatabaseId); } if (databaseCreationResponse.StatusCode == System.Net.HttpStatusCode.Accepted || databaseCreationResponse.StatusCode == System.Net.HttpStatusCode.OK || databaseCreationResponse.StatusCode == System.Net.HttpStatusCode.Created) { await CreateContainerIfNotExistsAsync(client, options); } else { throw new NotSupportedException($"Database '{options.DatabaseId}' cannot be created. Wrong Url or Key? ({databaseCreationResponse.StatusCode})"); } }
public static CosmosContainerProvider CreateFromOptions <TEntity>(ICosmosContainerOptions <TEntity> options) { return(new CosmosContainerProvider(options)); }
public static async Task CreateContainerIfNotExistsAsync(CosmosClient client, ICosmosContainerOptions options) { var partitionKey = options.PartitionKey; if (!string.IsNullOrEmpty(partitionKey)) { if (!partitionKey.StartsWith("/")) { partitionKey = "/" + partitionKey; } } var database = client.GetDatabase(options.DatabaseId); var definition = database.DefineContainer(options.ContainerId, partitionKey); if (options.DefaultTTLInSeconds.HasValue) { definition.WithDefaultTimeToLive(options.DefaultTTLInSeconds.Value); } definition.Build(); await definition.CreateIfNotExistsAsync(options.ProvisionOnDatabaseLevel?(int?)null : options.RequestUnits); await Task.Delay(500); }