public async Task SeedAsync() { try { if (settings.MasterSeedEnabled) { try { await settings.CosmosDb.ValidateObjectAsync(); } catch (Exception ex) { throw new InvalidConfigException("The Cosmos DB configuration is required to create the master tenant documents.", ex); } var db = await repositoryClient.Client.CreateDatabaseIfNotExistsAsync(new Database { Id = repositoryClient.DatabaseId }); if (db.StatusCode == HttpStatusCode.Created) { var partitionKeyDefinition = new PartitionKeyDefinition { Paths = new Collection <string> { "/partition_id" } }; var documentCollection = new DocumentCollection { Id = repositoryClient.CollectionId, PartitionKey = partitionKeyDefinition }; var ttlDocumentCollection = new DocumentCollection { Id = repositoryClient.TtlCollectionId, PartitionKey = partitionKeyDefinition, DefaultTimeToLive = -1 }; if (repositoryClient.CollectionId == repositoryClient.TtlCollectionId) { _ = await repositoryClient.Client.CreateDocumentCollectionIfNotExistsAsync(repositoryClient.DatabaseUri, ttlDocumentCollection); logger.Trace("One Cosmos DB Document Collection created."); } else { _ = await repositoryClient.Client.CreateDocumentCollectionIfNotExistsAsync(repositoryClient.DatabaseUri, documentCollection); _ = await repositoryClient.Client.CreateDocumentCollectionIfNotExistsAsync(repositoryClient.DatabaseUri, ttlDocumentCollection); logger.Trace("Two Cosmos DB Document Collections created."); } await masterTenantDocumentsSeedLogic.SeedAsync(); } } } catch (Exception ex) { logger.CriticalError(ex, "Error seeding master documents."); throw; } }
private async Task CreateDocumentClient() { var jsonSerializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Include, ContractResolver = new CamelCasePropertyNamesContractResolver(), }; jsonSerializerSettings.Converters.Add(new StringEnumConverter(typeof(CamelCaseNamingStrategy))); Client = new DocumentClient(serviceEndpont, settings.CosmosDb.PrimaryKey, jsonSerializerSettings, new ConnectionPolicy { ConnectionMode = ConnectionMode.Direct, ConnectionProtocol = Protocol.Tcp }); if (environment.IsDevelopment()) { await Client.CreateDatabaseIfNotExistsAsync(new Database { Id = DatabaseId }); var partitionKeyDefinition = new PartitionKeyDefinition { Paths = new Collection <string> { "/partition_id" } }; var documentCollection = new DocumentCollection { Id = CollectionId, PartitionKey = partitionKeyDefinition }; var ttlDocumentCollection = new DocumentCollection { Id = TtlCollectionId, PartitionKey = partitionKeyDefinition, DefaultTimeToLive = -1 }; if (CollectionId == TtlCollectionId) { DocumentCollection = TtlDocumentCollection = await Client.CreateDocumentCollectionIfNotExistsAsync(databaseUri, ttlDocumentCollection); logger.Trace("One Cosmos DB Document Collection created."); } else { DocumentCollection = await Client.CreateDocumentCollectionIfNotExistsAsync(databaseUri, documentCollection); TtlDocumentCollection = await Client.CreateDocumentCollectionIfNotExistsAsync(databaseUri, ttlDocumentCollection); logger.Trace("Two Cosmos DB Document Collections created."); } } }
public async Task SeedAsync() { try { if (settings.MasterSeedEnabled) { try { await settings.CosmosDb.ValidateObjectAsync(); } catch (Exception ex) { throw new InvalidConfigException("The Cosmos DB configuration is required to create the master tenant documents.", ex); } var databaseResponse = await repositoryClient.Client.CreateDatabaseIfNotExistsAsync(settings.CosmosDb.DatabaseId); if (databaseResponse.StatusCode == HttpStatusCode.Created) { if (settings.CosmosDb.ContainerId == settings.CosmosDb.TtlContainerId) { var container = await databaseResponse.Database.CreateContainerIfNotExistsAsync( new ContainerProperties { Id = settings.CosmosDb.TtlContainerId, PartitionKeyPath = Constants.Models.CosmosPartitionKeyPath, DefaultTimeToLive = -1 }); logger.Trace("One Cosmos DB Document container created."); (repositoryClient as RepositoryClientBase).SetContainers(container, container); } else { var container = await databaseResponse.Database.CreateContainerIfNotExistsAsync(new ContainerProperties { Id = settings.CosmosDb.ContainerId, PartitionKeyPath = Constants.Models.CosmosPartitionKeyPath }); var ttlContainer = await databaseResponse.Database.CreateContainerIfNotExistsAsync( new ContainerProperties { Id = settings.CosmosDb.TtlContainerId, PartitionKeyPath = Constants.Models.CosmosPartitionKeyPath, DefaultTimeToLive = -1 }); logger.Trace("Two Cosmos DB Document containers created."); (repositoryClient as RepositoryClientBase).SetContainers(container, ttlContainer); } await masterTenantDocumentsSeedLogic.SeedAsync(); logger.Trace("Cosmos DB Document container(s) seeded."); } } } catch (Exception ex) { logger.CriticalError(ex, "Error seeding master documents."); throw; } }