static async Task InitializeContainers(IConfiguration configuration) { CosmosClient client = new CosmosClientBuilder(configuration.GetConnectionString("Cosmos")) .Build(); Database database = await client.CreateDatabaseIfNotExistsAsync("OnDotNet"); await database.CreateContainerIfNotExistsAsync("episode1serializer", "/id"); }
private async Task <Database> GetCosmosDatabase() { string endpoint = this.Configuration["CosmosEndpoint"]; string key = this.Configuration["CosmosKey"]; CosmosClient client = new CosmosClientBuilder(endpoint, key).Build(); return(await client.CreateDatabaseIfNotExistsAsync("zap")); }
static async Task Main(string[] args) { IConfiguration configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .Build(); // Fluent builder CosmosClient client = new CosmosClientBuilder(configuration.GetConnectionString("Cosmos")) .WithApplicationName("OnDotNetRocks") .WithConnectionModeGateway() .WithApplicationRegion(Regions.WestUS2) .WithConsistencyLevel(ConsistencyLevel.Session) .WithThrottlingRetryOptions( TimeSpan.FromSeconds(10), 5) .Build(); // Similar non-fluent initialization // CosmosClient client = new CosmosClient(configuration.GetConnectionString("Cosmos"), // new CosmosClientOptions(){ // ApplicationName = "OnDotNetRocks", // ApplicationRegion = Regions.WestUS2, // MaxRetryAttemptsOnRateLimitedRequests = 5, // MaxRetryWaitTimeOnRateLimitedRequests = TimeSpan.FromSeconds(10), // ConnectionMode = ConnectionMode.Gateway, // ConsistencyLevel = ConsistencyLevel.Session // }); Database database = await client.CreateDatabaseIfNotExistsAsync("OnDotNet"); // Define container Container container = await database.DefineContainer("episode1builder", "/pk") .WithDefaultTimeToLive(TimeSpan.FromSeconds(60)) .WithIndexingPolicy() .WithIndexingMode(IndexingMode.Consistent) .WithExcludedPaths() .Path("/excludeThis/*") .Path("/andExcludeThis/*") .Attach() .WithIncludedPaths() .Path("/*") .Attach() .WithCompositeIndex() .Path("/composite", CompositePathSortOrder.Ascending) .Path("/composite2", CompositePathSortOrder.Ascending) .Attach() .Attach() .WithUniqueKey() .Path("/uniqueKey") .Attach() .CreateIfNotExistsAsync(); }
static async Task Main(string[] args) { CosmosClient cosmosClient = new CosmosClientBuilder(endpointUri, primaryKey).Build(); Database database = await cosmosClient.CreateDatabaseIfNotExistsAsync(databaseName); await database.CreateContainerIfNotExistsAsync(new ContainerProperties(sourceContainerName, "/id")); await database.CreateContainerIfNotExistsAsync(new ContainerProperties(leaseContainerName, "/id")); ChangeFeedProcessor processor = await StartChangeFeedProcessorAsync(cosmosClient); await GenerateItemsAsync(cosmosClient); }
public CosmosDbService(Settings settings) { var db = settings.CosmosDbSettings.Database; var client = new CosmosClientBuilder(settings.CosmosDbSettings.Connection) .WithSerializerOptions(new CosmosSerializationOptions { PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase }) .Build(); var database = client.CreateDatabaseIfNotExistsAsync(db).Result; database.Database.CreateContainerIfNotExistsAsync(TRANSACTION_CONTAINER, "/location").Wait(); _transactionContainer = client.GetContainer(db, TRANSACTION_CONTAINER); }
private static async Task <Container> CosmosContainerFactory() { string connectionString = "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="; var serializerOptions = new CosmosSerializationOptions { PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase }; CosmosClient client = new CosmosClientBuilder(connectionString).WithSerializerOptions(serializerOptions).Build(); await client.CreateDatabaseIfNotExistsAsync("UnitTesting"); Database database = client.GetDatabase("UnitTesting"); var containerProperties = new ContainerProperties(id: "Default", partitionKeyPath: "/id"); await database.CreateContainerIfNotExistsAsync(containerProperties); return(database.GetContainer("Default")); }
public static async Task Main(string[] args) { var configurationRoot = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddEnvironmentVariables() .Build(); var client = new CosmosClientBuilder(configurationRoot["ZeroFrictionDatabaseConnectionString"]) .WithSerializerOptions(new CosmosSerializationOptions { PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase }) .Build(); await client.CreateDatabaseIfNotExistsAsync("zerofriction"); var database = client.GetDatabase("zerofriction"); await database.CreateContainerIfNotExistsAsync("customers", "/tenantId"); await database.CreateContainerIfNotExistsAsync("invoices", "/tenantId"); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddLogging(logBuilder => { logBuilder.AddConsole(); }); services.AddControllers(options => { options.Filters.Add(typeof(ApiExceptionFilter)); }); services.AddSingleton <IDatabaseProvider, DatabaseProvider>(provider => { var connectionString = GetSetting(provider, "StorageConnectionString"); var logger = provider.GetService <ILogger <ApplicationLogs> >(); services.AddSingleton(typeof(ILogger), logger); var client = new CosmosClientBuilder(connectionString) .WithSerializerOptions(new CosmosSerializationOptions { PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase }) .Build(); var resp = client.CreateDatabaseIfNotExistsAsync("ImageGallery").Result; return(new DatabaseProvider(resp.Database)); }); services.AddSingleton <IBlobContainerProvider, BlobContainerProvider>(provider => { var connectionString = GetSetting(provider, "BlobStorageConnectionString"); return(new BlobContainerProvider(connectionString)); }); }
private async Task <CosmosClient> InitializeCosmosDb(CosmosConfiguration cosmosConfiguration) { var cosmosClient = new CosmosClientBuilder(cosmosConfiguration.Endpoint, cosmosConfiguration.Key) .WithBulkExecution(true) .WithConnectionModeDirect() .WithThrottlingRetryOptions(TimeSpan.FromSeconds(10), 100) .Build(); var databaseResponse = await cosmosClient.CreateDatabaseIfNotExistsAsync(cosmosConfiguration.Database); // Set up containers await databaseResponse.Database .DefineContainer(DataConstants.USER_CONTAINER, PartitionConstants.PARTITION_KEY) .WithIndexingPolicy() .WithIndexingMode(IndexingMode.Consistent) .WithIncludedPaths() .Path("/PartititionKey/?") .Path("/CreatedDateTimeUTC/?") .Path("/EntityType/?") .Path("/Category/?") .Path("/Subcategory/?") .Path("/FinancialAccountId/?") .Attach() .WithExcludedPaths() .Path("/*") .Attach() .Attach() .CreateIfNotExistsAsync(cosmosConfiguration.Throughput); await databaseResponse.Database .DefineContainer(DataConstants.USER_INSTITUTION_CONTAINER, PartitionConstants.PARTITION_KEY) .WithIndexingPolicy() .WithIndexingMode(IndexingMode.Consistent) .WithIncludedPaths() .Path("/PartititionKey/?") .Path("/CreatedDateTimeUTC/?") .Path("/EntityType/?") .Attach() .WithExcludedPaths() .Path("/*") .Attach() .Attach() .CreateIfNotExistsAsync(cosmosConfiguration.Throughput); await databaseResponse.Database .DefineContainer(DataConstants.FINANCIAL_ACCOUNT_CONTAINER, PartitionConstants.PARTITION_KEY) .WithIndexingPolicy() .WithIndexingMode(IndexingMode.Consistent) .WithIncludedPaths() .Path("/PartititionKey/?") .Path("/CreatedDateTimeUTC/?") .Path("/EntityType/?") .Path("/Category/?") .Path("/Subcategory/?") .Path("/FinancialAccountId/?") .Attach() .WithExcludedPaths() .Path("/*") .Attach() .Attach() .CreateIfNotExistsAsync(cosmosConfiguration.Throughput); await databaseResponse.Database .DefineContainer(DataConstants.CATEGORY_CONTAINER, PartitionConstants.PARTITION_KEY) .WithIndexingPolicy() .WithIndexingMode(IndexingMode.Consistent) .WithIncludedPaths() .Path("/PartititionKey/?") .Path("/CreatedDateTimeUTC/?") .Attach() .WithExcludedPaths() .Path("/*") .Attach() .Attach() .CreateIfNotExistsAsync(cosmosConfiguration.Throughput); await databaseResponse.Database .DefineContainer(DataConstants.INSTITUTION_CONTAINER, PartitionConstants.PARTITION_KEY) .WithIndexingPolicy() .WithIndexingMode(IndexingMode.Consistent) .WithIncludedPaths() .Path("/PartititionKey/?") .Path("/CreatedDateTimeUTC/?") .Attach() .WithExcludedPaths() .Path("/*") .Attach() .Attach() .CreateIfNotExistsAsync(cosmosConfiguration.Throughput); return(cosmosClient); }