Esempio n. 1
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;
        }
Esempio n. 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();
        }
Esempio n. 3
0
        // 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();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            if (Configuration.GetValue <bool>("SeedDatabase"))
            {
                using (IServiceScope scope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope())
                {
                    IDataInitializer dataInitializer = scope.ServiceProvider.GetRequiredService <IDataInitializer>();
                    dataInitializer.InitializeData();
                }
            }


            app.UseStaticFiles();
            app.UseHttpsRedirection();
            app.UseCookiePolicy();
            app.UseNToastNotify();
            app.UseAuthentication();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=account}/{action=login}/{id?}");
            });
        }
Esempio n. 4
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());
        }
Esempio n. 5
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IDataInitializer dataInitializer)
        {
            if (env.IsDevelopment())
            {
                dataInitializer.Seed();
            }
            if (env.IsDevelopment() || env.IsStaging())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseStaticFiles();

            app.UseSerilogRequestLogging();

            app.UseHttpsRedirection();
            app.UseRouting();
            app.UseAuthorization();

            app.UseOpenApi();
            app.UseSwaggerUi3(cfg =>
            {
                cfg.CustomStylesheetPath = "/css/swaggercustom.css";
            });

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
Esempio n. 6
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, ApplicationDbContext context,
                              IExporterProvider exporterProvider)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseAuthentication();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

            context.Database.Migrate();
            dataInitializer.Initialize();
        }
Esempio n. 7
0
 public EfDataSeeder(SalesContext context, IUnitOfWork unitOfWork,
                     IDataInitializer dataInitializer, IOptions <SqlOptions> options)
 {
     _context         = context;
     _unitOfWork      = unitOfWork;
     _dataInitializer = dataInitializer;
     _options         = options;
 }
Esempio n. 8
0
 public DataInitializer(
     IDataInitializer <AppDataStore> initializer,
     BlogAccess blogAccess,
     PostAccess postAccess
     )
 {
     this.initializer = initializer;
     this.blogAccess  = blogAccess;
     this.postAccess  = postAccess;
 }
Esempio n. 9
0
 public Seeder(
     IDataInitializer dataInitializer,
     IIdentityService identityService,
     IWebsiteService websiteService,
     IIdentityRepository identityRepository,
     IConfigurationRoot configuration)
 {
     _dataInitializer    = dataInitializer;
     _identityService    = identityService;
     _websiteService     = websiteService;
     _identityRepository = identityRepository;
     _configuration      = configuration;
 }
        public async static Task Initialize(this IApplicationBuilder app)
        {
            using (IServiceScope scope = app.ApplicationServices.CreateScope())
            {
                using (ApplicationDbContext applicationDbContext = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>())
                {
                    await applicationDbContext.Database.MigrateAsync();

                    IDataInitializer dataInitializer = scope.ServiceProvider.GetRequiredService <IDataInitializer>();

                    await dataInitializer.Initialize();
                }
            }
        }
 public static SchoolDbContext GetSchoolDbContext(IDataInitializer dataInitializer = null)
 {
     if (_schoolDbContext == null)
     {
         DbContextOptions <SchoolDbContext> options;
         var builder = new DbContextOptionsBuilder <SchoolDbContext>();
         builder.UseSqlite(CreateInMemoryDatabase());
         options          = builder.Options;
         _schoolDbContext = new SchoolDbContext(options);
         if (dataInitializer == null)
         {
             dataInitializer = new DataInitializer();
         }
         dataInitializer.Initialize(_schoolDbContext);
     }
     return(_schoolDbContext);
 }
Esempio n. 12
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, ILoggerFactory loggerFactory)
        {
            dataInitializer.Run();

            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");

                // For more details on creating database during deployment see http://go.microsoft.com/fwlink/?LinkID=615859
                try
                {
                    using (var serviceScope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>()
                                              .CreateScope())
                    {
                        serviceScope.ServiceProvider.GetService <ApplicationDbContext>()
                        .Database.Migrate();
                    }
                }
                catch { }
            }

            app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear());

            app.UseStaticFiles();

            app.UseIdentity();

            // To configure external authentication please see http://go.microsoft.com/fwlink/?LinkID=532715

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
Esempio n. 13
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, ILoggerFactory loggerFactory)
        {
            dataInitializer.Run();

            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");

                // For more details on creating database during deployment see http://go.microsoft.com/fwlink/?LinkID=615859
                try
                {
                    using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
                        .CreateScope())
                    {
                        serviceScope.ServiceProvider.GetService<ApplicationDbContext>()
                             .Database.Migrate();
                    }
                }
                catch { }
            }

            app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear());

            app.UseStaticFiles();

            app.UseIdentity();

            // To configure external authentication please see http://go.microsoft.com/fwlink/?LinkID=532715

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
Esempio n. 14
0
        public static IServiceCollection AddPhotoSafeData(
            this IServiceCollection services,
            string connectionString,
            IDataInitializer dataInitializer = null)
        {
            services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext <ApplicationDbContext>(options =>
                                                 options.UseSqlServer(connectionString));

            services.AddIdentity <ApplicationUser, ApplicationRole>()
            .AddEntityFrameworkStores <ApplicationDbContext>()
            .AddDefaultTokenProviders();

            if (dataInitializer != null)
            {
                dataInitializer.Run();
            }

            return(services);
        }
Esempio n. 15
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);
            }
        }
Esempio n. 16
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IDataInitializer dataInitializer)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                dataInitializer.Seed();
            }

            app.UseRouting();

            app.UseHttpsRedirection();
            app.UseStaticFiles();

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

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
        // This method gets called by the runtime. Use this method to configure the HTTP EmployeeDto pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IDataInitializer dataInitializer)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseMiddleware <ExceptionHandlerMiddleware>();
            }

            var settings = app.ApplicationServices.GetService <IOptions <XmlRepositorySettings> >();

            if (settings.Value.SeedData)
            {
                dataInitializer.SeedData();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc();
        }
 public HomeController(IDataInitializer dataInitializer, ApplicationDbContext _context)
 {
     this.dataInitializer = dataInitializer;
     this._context        = _context;
 }
Esempio n. 19
0
 internal ApiConfiguration(IDataInitializer dataInitializer)
 {
     this.DataInitializer = dataInitializer;
 }
 public BackendCoreDbContext(DbContextOptions <BackendCoreDbContext> options, IDataInitializer dataInitializer) : base(options)
 {
     _dataInitializer = dataInitializer;
 }
Esempio n. 21
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IDataInitializer dataInitializer,
                              ISchedulerDataLoader schedulerDataLoader)
        {
            if (/*env.IsDevelopment()*/ true)
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/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.UseSpa(spa =>
            {
                // To learn more about options for serving an Angular SPA from ASP.NET Core,
                // see https://go.microsoft.com/fwlink/?linkid=864501

                spa.Options.SourcePath = "ClientApp";

                if (env.IsDevelopment())
                {
                    //spa.UseProxyToSpaDevelopmentServer("http://localhost:4200");
                    spa.UseAngularCliServer(npmScript: "start");
                }
            });

            app.UseCors("AllowAll");

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            if (!env.IsDevelopment())
            {
                app.UseSpaStaticFiles();
            }
            else
            {
                dataInitializer.Seed();
            }

            schedulerDataLoader.Initialize();
            app.UseRouting();

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

            app.UseMiddleware <LogHttpContextMiddleware>();
            // Enable middleware to serve generated Swagger as a JSON endpoint.
            app.UseSwagger();

            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
            // specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); });

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller}/{action=Index}/{id?}");
            });
        }
 public StudentRepo_Fake(IDataInitializer dataInitializer)
 {
     this.dataInitializer = dataInitializer;
 }
Esempio n. 23
0
 public MyDbContext(DbContextOptions <MyDbContext> options, IDataInitializer dataInitializer) : base(options)
 {
     _dataInitializer = dataInitializer;
 }
Esempio n. 24
0
 public StudentRepo_Fake(IDataInitializer fake_context)
 {
     context = fake_context;
 }
 public HomeController(ILogger <HomeController> logger, IDataInitializer dataini)
 {
     _logger          = logger;
     _dataInitializer = dataini;
 }
Esempio n. 26
0
 public InMemoryDataSeeder(IDataInitializer dataInitializer)
 {
     _dataInitializer = dataInitializer;
 }
Esempio n. 27
0
        /// <summary>
        /// 初始化数据库
        /// </summary>
        public static void InitDataBase()
        {
            IDataInitializer initializer = ResolveMediator.Resolve <IDataInitializer>();

            initializer.Initialize();
        }
 /*------------------------ METHODS REGION ------------------------*/
 public SampleController(IDataInitializer dataInitializer)
 {
     _dataInitializer = dataInitializer;
 }