Пример #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, IApplicationLifetime appLifetime)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // 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.UseAuthentication();

            GeneralSettings settings =
                app.ApplicationServices.GetService <GeneralSettings>();

            if (settings.SeedData)
            {
                IDataInitializer dataInitializer =
                    app.ApplicationServices.GetService <IDataInitializer>();
                dataInitializer.SeedAsync();
            }

            app.UseMvc();
            appLifetime.ApplicationStopped.Register(() => ApplicationContainer.Dispose());
        }
Пример #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IDataInitializer dataInitializer)
        {
            app.UseStaticFiles();
            app.UseAuthentication();
            app.UseDeveloperExceptionPage();
            app.UseCustomExceptionHandler();

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

            app.UseMvc();

            app.UseDirectoryBrowser(new DirectoryBrowserOptions
            {
                FileProvider = new PhysicalFileProvider(
                    Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images")),
                RequestPath = "/images/restaurants"
            });

            dataInitializer.SeedAsync().Wait();
        }
Пример #3
0
        public static IWebHost InitializeDatabase(this IWebHost host, IDataInitializer dataInitializer)
        {
            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                var logger = services.GetRequiredService<ILogger<IDataInitializer>>();

                try
                {
                    logger.LogInformation($"Migrating the database...");

                    var retry = Policy.Handle<SqlException>()
                         .WaitAndRetry(new TimeSpan[]
                         {
                             TimeSpan.FromSeconds(3),
                             TimeSpan.FromSeconds(5),
                             TimeSpan.FromSeconds(8),
                             TimeSpan.FromSeconds(13),
                         });

                    retry.Execute(()=>dataInitializer.SeedAsync(scope.ServiceProvider).Wait());

                    logger.LogInformation($"Database migrated!");

                }
                catch(Exception ex)
                {
                    logger.LogError(ex, $"An error occurred while migrating the database.");
                }
               
            }

            return host;
        }
Пример #4
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.UseAuthorization();

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

            IDataInitializer initializer = app.ApplicationServices.GetService <IDataInitializer>();

            if (initializer != null)
            {
                initializer.SeedAsync(15);
            }
        }