// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); using (var scope = app.ApplicationServices.CreateScope()) { var ctx = scope.ServiceProvider.GetService <PetShopAppContext>(); DBInitilizer.SeedDB(ctx); } } else { using (var scope = app.ApplicationServices.CreateScope()) { var ctx = scope.ServiceProvider.GetService <PetShopAppContext>(); ctx.Database.EnsureCreated(); } app.UseHsts(); } app.UseHttpsRedirection(); // Enable CORS (must precede app.UseMvc()): app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()); // Use authentication app.UseAuthentication(); //app.UseHttpsRedirection(); app.UseMvc(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider, ILoggerFactory loggerFactory) { loggerFactory.AddNLog(); //add NLog.Web app.AddNLogWeb(); app.UseRequestIPMiddleware(); ClientAppPath = Path.Combine(Directory.GetCurrentDirectory(), ClientAppPath); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions { HotModuleReplacement = true, ProjectPath = ClientAppPath, ConfigFile = $"{ClientAppPath}webpack.config.js", HotModuleReplacementEndpoint = "/dist/__webpack_hmr" }); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseAuthentication(); // Seed the database with the sample applications. // Note: in a real world application, this step should be part of a setup script. InitializeAsync(app.ApplicationServices, CancellationToken.None).GetAwaiter().GetResult(); if (env.IsDevelopment()) { // init roles and users etc DBInitilizer.Initialize(serviceProvider); GroupsInit.Initialize(serviceProvider); FileCategoriesInit.Initialize(serviceProvider); } app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); routes.MapSpaFallbackRoute( name: "spa-fallback", defaults: new { controller = "Home", action = "Index" }); }); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider, ILoggerFactory loggerFactory) { loggerFactory.AddNLog(); app.UseServerSentEventsMiddleware(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseWebpackDevMiddleware( new WebpackDevMiddlewareOptions { HotModuleReplacement = true, ConfigFile = "./build/webpack.config.js" }); } else { app.UseHsts(); } app.UseHttpsRedirection(); app.UseResponseCompression(); // No need if you use IIS, but really something good for Kestrel! app.UseAuthentication(); // Seed the database with the sample applications. // Note: in a real world application, this step should be part of a setup script. InitializeAsync(app.ApplicationServices, CancellationToken.None).GetAwaiter().GetResult(); if (env.IsDevelopment()) { // init roles and users etc DBInitilizer.Initialize(serviceProvider); GroupsInit.Initialize(serviceProvider); FileCategoriesInit.Initialize(serviceProvider); } // Idea: https://code.msdn.microsoft.com/How-to-fix-the-routing-225ac90f // This avoid having a real mvc view. You have other way of doing, but this one works // properly. app.UseSpa(); app.UseDefaultFiles(); app.UseStaticFiles(); app.UseMvc(); }
public static void Main(string[] args) { var host = BuildWebHost(args); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; try { var context = services.GetRequiredService <ApplicationDbContext>(); DBInitilizer.Initialize(context); } catch (Exception ex) { var logger = services.GetRequiredService <ILogger <Program> >(); logger.LogError(ex, "An error occurred while seeding the database."); } } host.Run(); }