public static void Main(string[] args) { var host = CreateHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; try { try { //var userManager = services.GetRequiredService<UserManager<IdentityUser>>(); var roleManager = services.GetRequiredService <RoleManager <IdentityRole> >(); SeedRoles.Initialize(services, roleManager); } catch (Exception ex) { Debug.WriteLine(ex.Message); } var context = services.GetRequiredService <ApplicationDbContext>(); // context.Database.Migrate(); } catch (Exception ex) { var logger = services.GetRequiredService <ILogger <Program> >(); logger.LogError(ex, "An error occurred seeding the DB."); } } host.Run(); }
public static void Main(string[] args) { IWebHost host = BuildWebHost(args); using (IServiceScope scope = host.Services.CreateScope()) { IServiceProvider services = scope.ServiceProvider; UserManager <ApplicationUser> userManager = services.GetRequiredService <UserManager <ApplicationUser> >(); try { #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed Task rolesTask = SeedRoles.Initialize(services, userManager); Task productsTask = SeedProducts.Initialize(services); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed // Spinlock until seeding is complete (there is surely a better way to do this?) while (!rolesTask.IsCompleted || !productsTask.IsCompleted) { } } catch { Console.Error.WriteLine( "Could not seed database with admin user and roles."); throw; } } host.Run(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); } else { app.UseExceptionHandler("/Home/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseSession(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); endpoints.MapRazorPages(); }); SeedRoles.Initialize(serviceProvider).GetAwaiter().GetResult(); SeedUsers.Initialize(serviceProvider).GetAwaiter().GetResult(); }