diff --git a/src/Services/Catalog/Catalog.API/Data/CatalogContext.cs b/src/Services/Catalog/Catalog.API/Data/CatalogContext.cs index 453ff733..9022c4e0 100644 --- a/src/Services/Catalog/Catalog.API/Data/CatalogContext.cs +++ b/src/Services/Catalog/Catalog.API/Data/CatalogContext.cs @@ -11,9 +11,7 @@ public CatalogContext(IConfiguration configuration) { var client = new MongoClient(configuration.GetValue("DatabaseSettings:ConnectionString")); var database = client.GetDatabase(configuration.GetValue("DatabaseSettings:DatabaseName")); - Products = database.GetCollection(configuration.GetValue("DatabaseSettings:CollectionName")); - CatalogContextSeed.SeedData(Products); } public IMongoCollection Products { get; } diff --git a/src/Services/Catalog/Catalog.API/Data/CatalogContextSeed.cs b/src/Services/Catalog/Catalog.API/Data/CatalogContextSeed.cs index d61b31e9..a986602d 100644 --- a/src/Services/Catalog/Catalog.API/Data/CatalogContextSeed.cs +++ b/src/Services/Catalog/Catalog.API/Data/CatalogContextSeed.cs @@ -7,12 +7,20 @@ namespace Catalog.API.Data { public class CatalogContextSeed { - public static void SeedData(IMongoCollection productCollection) + public static void PrepPopulation(IApplicationBuilder app) { - bool existProduct = productCollection.Find(p => true).Any(); + using (var serviceScope = app.ApplicationServices.CreateScope()) + { + SeedData(serviceScope.ServiceProvider.GetService()); + } + } + + public static void SeedData(ICatalogContext catalogContext) + { + bool existProduct = catalogContext.Products.Find(p => true).Any(); if (!existProduct) { - productCollection.InsertManyAsync(GetPreconfiguredProducts()); + catalogContext.Products.InsertManyAsync(GetPreconfiguredProducts()); } } diff --git a/src/Services/Catalog/Catalog.API/Startup.cs b/src/Services/Catalog/Catalog.API/Startup.cs index 236d75dd..e2a443a4 100644 --- a/src/Services/Catalog/Catalog.API/Startup.cs +++ b/src/Services/Catalog/Catalog.API/Startup.cs @@ -62,6 +62,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse }); }); + CatalogContextSeed.PrepPopulation(app); } } }