private static void SeedDatabase(IHost host) { using var scope = host.Services.CreateScope(); var productContext = scope.ServiceProvider.GetRequiredService <ProductContext>(); ProductContextSeed.SeedAsync(productContext); }
public ProductContext(IProductDatabaseSettings settings) { var client = new MongoClient(settings.ConnectionString); var database = client.GetDatabase(settings.DatabaseName); Products = database.GetCollection <Entites.Product>(settings.CollectionName); ProductContextSeed.SeedData(Products); }
private async Task WaitForSqlAvailabilityAsync(IApplicationBuilder app, IHostingEnvironment env, int retries = 0) { var policy = CreatePolicy(retries, nameof(WaitForSqlAvailabilityAsync)); await policy.ExecuteAsync(async() => { var context = (ProductContext)app .ApplicationServices.GetService(typeof(ProductContext)); await ProductContextSeed.SeedAsync(context); }); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IApiVersionDescriptionProvider provider, IServiceProvider services ) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseHttpsRedirection(); } app.UseCors("CorsPolicy"); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); app.UseSwagger(); app.UseSwaggerUI(options => { foreach (var description in provider.ApiVersionDescriptions) { options.SwaggerEndpoint( $"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant() ); } }); ProductContextSeed.SeedAsync( services.GetRequiredService <ProductContext>(), services.GetRequiredService <ILogger <ProductContextSeed> >() ).ConfigureAwait(true); }