public void Configure(IApplicationBuilder app, IWebHostEnvironment env, SeedDb seeder) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); seeder.SeedAsync().Wait(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); endpoints.MapRazorPages(); }); }
private static void RunSeeding(IHost host) { IServiceScopeFactory scopeFactory = host.Services.GetService <IServiceScopeFactory>(); using IServiceScope scope = scopeFactory.CreateScope(); SeedDb seeder = scope.ServiceProvider.GetService <SeedDb>(); seeder.SeedAsync().Wait(); }
private static void RunSeeding(IWebHost host) { var scopeFactory = host.Services.GetService <IServiceScopeFactory>(); using (var scope = scopeFactory.CreateScope()) { SeedDb seeder = scope.ServiceProvider.GetService <SeedDb>(); seeder.SeedAsync().Wait(); //Wait till data is added to DB for Testing } }
private static void RunSeeding(IWebHost host) { IServiceScopeFactory scopeFactory = host.Services.GetService <IServiceScopeFactory>(); using (IServiceScope scope = scopeFactory.CreateScope()) { SeedDb seeder = scope.ServiceProvider.GetService <SeedDb>(); seeder.SeedAsync().Wait(); //Wait, permite correr un mètodo no async en async } }
private static void RunSeeding(IWebHost host) { IServiceScopeFactory scopeFactory = host.Services.GetService <IServiceScopeFactory>(); using (IServiceScope scope = scopeFactory.CreateScope()) { SeedDb seeder = scope.ServiceProvider.GetService <SeedDb>(); try { seeder.SeedAsync().Wait(); } catch (Exception) { } } }
public static void Main(string[] args) { var host = CreateHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { try { var recipesDbContext = scope.ServiceProvider.GetRequiredService <RecipesDbContext>(); var userManager = scope.ServiceProvider.GetRequiredService <UserManager <AppUser> >(); recipesDbContext.Database.Migrate(); SeedDb.SeedAsync(recipesDbContext, userManager).Wait(); } catch (Exception ex) { scope.ServiceProvider.GetRequiredService <ILogger>().LogError(ex, "Error running the app"); } } host.Run(); }