Example #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(
            IApplicationBuilder app,
            IWebHostEnvironment env,
            SeedInitialData seeder)
        {
            // If, for some reason, you need a reference to the built container, you
            // can use the convenience extension method GetAutofacRoot.
            //this.AutofacContainer = app.ApplicationServices.GetAutofacRoot();

            AppDependencyResolverService.Init(app.ApplicationServices);


            // If, for some reason, you need a reference to the built container, you
            // can use the convenience extension method GetAutofacRoot.
            this.AutofacContainer = app.ApplicationServices.GetAutofacRoot();


            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseCors(policy =>
            {
                policy
                .AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader();
            });

            app.UseHttpsRedirection();

            app.UseRouting();

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

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

            seeder.SeedUsers();

            app.UsarSwagger();
        }
        public static async Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;

                try
                {
                    var context = services.GetRequiredService <ApplicationDbContext>();

                    await context.Database.MigrateAsync();

                    await SeedInitialData.SeedAuthorsDataAsync(context);

                    await SeedInitialData.SeedCategoriesDataAsync(context);

                    await SeedInitialData.SeedBooksDataAsync(context);

                    // Identity
                    var userManager     = services.GetRequiredService <UserManager <AppUser> >();
                    var roleManager     = services.GetRequiredService <RoleManager <IdentityRole> >();
                    var identityContext = services.GetRequiredService <AppIdentityDbContext>();
                    var config          = services.GetRequiredService <IConfiguration>();

                    await identityContext.Database.MigrateAsync();

                    await SeedIdentityData.SeedUsersAndRolesAsync(userManager, roleManager, config);
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred during data seeding");
                }
            }

            await host.RunAsync();
        }