コード例 #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, TrainingDbContext context)
        {
            //TrainingSeedData.Initialize(context);
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();

                //app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
                //{
                //    HotModuleReplacement = true
                //});
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
            app.UseCors("angular");
            app.UseStaticFiles();


            using (var serviceScope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope())
            {
                var dbContext   = serviceScope.ServiceProvider.GetService <ApplicationDbContext>();
                var roleManager = serviceScope.ServiceProvider.GetService <RoleManager <ApplicationRole> >();
                var userManager = serviceScope.ServiceProvider.GetService <UserManager <ApplicationUser> >();

                dbContext.Database.Migrate();
                UserDataSeeder.SeedIdentityData(dbContext, roleManager, userManager);
            }
            app.UseAuthentication();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");

                routes.MapSpaFallbackRoute(
                    name: "spa-fallback",
                    defaults: new { controller = "Home", action = "Index" });
            });
        }
コード例 #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            //app.UseHttpsRedirection();

            app.UseRouting();

            app.UseCors("AllowAll");

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(
                    Path.Combine(Directory.GetCurrentDirectory(), "Resources")),
                RequestPath = "/Resources"
            });

            string stage = Environment.GetEnvironmentVariable("STAGE") ?? "development";
            string host  = Environment.GetEnvironmentVariable("DATABASE_TYPE") ?? "localhost";

            using (var scope = app.ApplicationServices.CreateScope())
                using (var context = scope.ServiceProvider.GetService <UserDbContext>())
                {
                    RelationalDatabaseCreator databaseCreator = (RelationalDatabaseCreator)context.Database.GetService <IDatabaseCreator>();


                    try
                    {
                        if (!stage.Equals("development") && host.Equals("postgres"))
                        {
                            databaseCreator.CreateTables();
                        }
                        else
                        {
                            context.Database.Migrate();
                        }
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Failed to execute migration");
                    }
                    try
                    {
                        UserDataSeeder seeder = new UserDataSeeder();
                        if (!seeder.IsAlreadyFull(context))
                        {
                            seeder.SeedAllEntities(context);
                        }
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Failed to seed data");
                    }
                }
        }