Exemplo n.º 1
0
        public static async Task Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();

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

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

                    var userManager = services.GetRequiredService <UserManager <IdentityUser> >();
                    await ApplicationIdentitySeedData.SeedAsync(userManager);
                }
                catch (SqlException e)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();

                    logger.LogError(e, "An error occurred creating the DB.");

                    throw;
                }
            }

            host.Run();
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            var host = BuildWebHost(args);

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                try
                {
                    var applicationDbContext           = services.GetRequiredService <ApplicationDbContext> ();
                    var applicationDbInitializerLogger = services.GetRequiredService <ILogger <ApplicationDbInitializer> > ();
                    ApplicationDbInitializer.Initialize(applicationDbContext, applicationDbInitializerLogger).Wait();

                    var userManager   = services.GetRequiredService <UserManager <ApplicationUser> > ();
                    var roleManager   = services.GetRequiredService <RoleManager <IdentityRole> > ();
                    var configuration = services.GetRequiredService <IConfiguration> ();
                    var identityDbInitializerLogger = services.GetRequiredService <ILogger <IdentityDbInitializer> > ();
                    IdentityDbInitializer.Initialize(userManager, roleManager, configuration).Wait();
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> > ();
                    logger.LogError(ex, "An error occurred while seeding the database.");
                }
            }

            host.Run();
        }
Exemplo 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,
            ApplicationDbContext context,
            RoleManager <IdentityRole> roleManager,
            UserManager <IdentityUser> userManager
            )
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/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.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseAuthentication();

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

            ApplicationDbInitializer.Initialize(context, roleManager, userManager);
        }
Exemplo 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)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/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.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
            using (var serviceScope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                using (var context = serviceScope.ServiceProvider.GetService <ApplicationDbContext>())
                {
                    ApplicationDbInitializer.Initialize(context);
                }
            }
        }
        public static void UseDataBase(this IApplicationBuilder app, IServiceProvider serviceProvider)
        {
            ApplicationDbContext applicationDbContext = serviceProvider.GetRequiredService <ApplicationDbContext>();

            applicationDbContext.Database.Migrate();

            ApplicationDbInitializer applicationDbInitializer = serviceProvider.GetRequiredService <ApplicationDbInitializer>();

            applicationDbInitializer.Initialize().Wait();
        }
Exemplo n.º 6
0
        public void TestInitialize()
        {
            var options = new DbContextOptionsBuilder <TimeTableContext>()
                          .UseInMemoryDatabase(databaseName: "InMemoryPopulated")
                          .Options;

            db = new TimeTableContext(options);

            // Load sample data for this test
            ApplicationDbInitializer.Initialize(db).Wait();
        }
Exemplo n.º 7
0
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var services = host.Services.CreateScope())
            {
                var db = services.ServiceProvider.GetRequiredService <ApplicationDbContext>();
                ApplicationDbInitializer.Initialize(db);
            }

            host.Run();
        }
Exemplo n.º 8
0
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                // Database initialization on startup
                ApplicationDbInitializer.Initialize(scope.ServiceProvider);
            }

            host.Run();
        }
Exemplo n.º 9
0
        public static void Main(string[] args)
        {
            IHostBuilder builder = CreateHostBuilder(args);
            IHost        host    = builder.Build();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                var context  = services.GetRequiredService <ApplicationDbContext>();
                _ = ApplicationDbInitializer.Initialize(context);
            }
            host.Run();
        }
Exemplo n.º 10
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(
            IApplicationBuilder app,
            IHostingEnvironment env,
            ILoggerFactory loggerFactory,
            ApplicationDbInitializer applicationDbInitializer)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            // Force redirect to HTTPS
            var options = new RewriteOptions()
                          .AddRedirectToHttps();

            app.UseRewriter(options);

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

            app.UseStaticFiles();

            app.UseIdentity();

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

            // Facebook authentucation
            app.UseFacebookAuthentication(new FacebookOptions()
            {
                AppId     = Configuration["Authentication:Facebook:AppId"],
                AppSecret = Configuration["Authentication:Facebook:AppSecret"]
            });

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

            applicationDbInitializer.Initialize().Wait();
        }
Exemplo n.º 11
0
        public static void Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services    = scope.ServiceProvider;
                var context     = services.GetRequiredService <ApplicationDbContext>();
                var environment = services.GetRequiredService <IHostingEnvironment>();

                ApplicationDbInitializer.Initialize(context, environment.IsDevelopment());
            }


            host.Run();
        }
Exemplo n.º 12
0
        public static void Main(string[] args)
        {
            var host = BuildWebHost(args);

            using (var scope = host.Services.CreateScope())
            {
                var services    = scope.ServiceProvider;
                var context     = services.GetRequiredService <ApplicationDbContext>();
                var userManager = services.GetRequiredService <UserManager <ApplicationUser> >();
                var env         = services.GetRequiredService <IHostingEnvironment>();

                ApplicationDbInitializer.Initialize(context, userManager, env.IsDevelopment());
            }

            host.Run();
        }
Exemplo n.º 13
0
        public static void Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();

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

                var db = services.GetRequiredService <ApplicationDbContext>();
                var um = services.GetRequiredService <UserManager <ApplicationUser> >();
                var rm = services.GetRequiredService <RoleManager <IdentityRole> >();

                ApplicationDbInitializer.Initialize(db, um, rm).Wait();
            }

            host.Run();
        }
Exemplo n.º 14
0
        public static async Task Main(string[] args)
        {
            var host = BuildWebHost(args);

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                try
                {
                    var context     = services.GetRequiredService <AccountDbContext>();
                    var userManager = services.GetRequiredService <UserManager <ApplicationUser> >();
                    var roleManager = services.GetRequiredService <RoleManager <IdentityRole> >();

                    var dbInitializerLogger = services.GetRequiredService <ILogger <AccountDbInitializer> >();
                    await AccountDbInitializer.Initialize(context, userManager, roleManager, dbInitializerLogger).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred while seeding the AccountDb database.");
                }
                try
                {
                    var context = services.GetRequiredService <ApplicationDbContext>();
                    await ApplicationDbInitializer.Initialize(context).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred while seeding the ApplicationDb database.");
                }
                try
                {
                    var contextOptions = services.GetRequiredService <DbContextOptions <HangfireDbContext> >();
                    await HangfireDbInitializer.InitializeAsync(contextOptions).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred while seeding the HangfireDb database.");
                }
            }

            host.Run();
        }
Exemplo n.º 15
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ApplicationDbInitializer initializer)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            #region localization
            var supportedCultures = new[]
            {
                new CultureInfo("en-US"),
                new CultureInfo("de-DE"),
                new CultureInfo("fr-FR")
            };

            app.UseRequestLocalization(new RequestLocalizationOptions
            {
                DefaultRequestCulture = new RequestCulture("de-DE"),
                // Formatting numbers, dates, etc.
                SupportedCultures = supportedCultures,
                // UI strings that we have localized.
                SupportedUICultures = supportedCultures
            });
            #endregion

            app.UseStaticFiles();

            app.UseAuthentication();

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

            initializer.Initialize().Wait();
        }
Exemplo n.º 16
0
        public static void Main(string[] args)
        {
            var env     = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            var hostUrl = string.Equals(env, "Development") ? "" : "http://*:80";

            var host = CreateWebHostBuilder(args).UseUrls(hostUrl).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services    = scope.ServiceProvider;
                var context     = services.GetRequiredService <ApplicationDbContext>();
                var userManager = services.GetRequiredService <UserManager <ApplicationUser> >();
                var environment = services.GetService <IWebHostEnvironment>();
                ApplicationDbInitializer.Initialize(context, userManager, environment.IsDevelopment());
            }

            host.Run();
        }
Exemplo n.º 17
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, AmazonContext amazonContext, DefaultUsersSeedData seeder, ApplicationDbContext appContext)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            //app.UseApplicationInsightsRequestTelemetry();
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
                app.UseBrowserLink();
            }

            app.UseStatusCodePagesWithRedirects("~/Home/Index");

            //app.UseApplicationInsightsExceptionTelemetry();

            app.UseStaticFiles();

            //app.UseIdentity();
            app.UseAuthentication();

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

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

            ApplicationDbInitializer.Initialize(appContext);
            AmazonDbInitializer.Initialize(amazonContext);

            seeder.AddDefaultUsers().Wait();
        }
Exemplo n.º 18
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, TimeTableContext context)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }

            app.UseStaticFiles();
            app.UseSpaStaticFiles();

            if (bool.Parse(Configuration["AddSampleData"].ToString()))
            {
                ApplicationDbInitializer.Initialize(context).Wait();
            }

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

            app.UseSignalR(routes =>
            {
                routes.MapHub <ScheduleHub>("/schedules");
            });

            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.UseAngularCliServer(npmScript: "start");
                }
            });
        }
Exemplo n.º 19
0
        private static void CreateDbIfNotExists(IHost host)
        {
            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;

                try
                {
                    var context = services.GetRequiredService <ApplicationDbContext>();
                    //context.Database.EnsureCreated();
                    ApplicationDbInitializer.Initialize(context);
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred creating the DB.");
                }
            }
        }
Exemplo n.º 20
0
        public static void Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope()) {
                var services = scope.ServiceProvider;
                try {
                    var context         = services.GetRequiredService <ApplicationDbContext>();
                    var serviceProvider = services.GetRequiredService <IServiceProvider>();
                    var configuration   = services.GetRequiredService <IConfiguration>();
                    ApplicationDbInitializer.Initialize(context, serviceProvider, configuration).Wait();
                } catch (Exception ex) {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred creating the DB.");
                }
            }

            host.Run();
        }
Exemplo n.º 21
0
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                try
                {
                    var concreteContext = scope.ServiceProvider.GetService <ApplicationDbContext>();
                    ApplicationDbInitializer.Initialize(concreteContext);
                }
                catch (Exception ex)
                {
                    var logger = scope.ServiceProvider.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred while migrating or initializing the database.");
                }
            }

            host.Run();
        }
Exemplo n.º 22
0
        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>();
                    ApplicationDbInitializer.Initialize(context);
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error database");
                }
            }
            host.Run();
        }
Exemplo n.º 23
0
        public static void Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();

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

                // Get our database context from the service provider
                var db = services.GetRequiredService <ApplicationDbContext>();

                // Get the UserManager and RoleManager also from the service provider
                var um = services.GetRequiredService <UserManager <ApplicationUser> >();
                var rm = services.GetRequiredService <RoleManager <IdentityRole> >();

                // Initialise the database using the initializer from Data/ExampleDbInitializer.cs
                ApplicationDbInitializer.Initialize(db, um, rm);
            }

            host.Run();
        }
Exemplo n.º 24
0
        public static void Main(string[] args)
        {
            // Setup logging first to catch all errors
            var logger =
                NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();

            try
            {
                logger.Debug("Starting up...");
                IHostBuilder builder = CreateHostBuilder(args);
                IHost        host    = builder.Build();

                // Seed the Database
                using (var scope = host.Services.CreateScope())
                {
                    var services = scope.ServiceProvider;
                    try
                    {
                        var context = services.GetRequiredService <ApplicationDbContext>();
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                        ApplicationDbInitializer.Initialize(context);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex, "An error occurred while seeding the database.");
                    }
                }

                host.Run();
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Program could not start due to error.");
                throw;
            }
        }
Exemplo n.º 25
0
        public static void Main(string[] args)
        {
            // Setup logging first to catch all errors
            var logger =
                NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();

            IWebHost host = null;

            try
            {
                logger.Debug("Starting up...");
                host = BuildWebHost(args);
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Program could not start due to error.");
                throw;
            }

            // Seed the Database
            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                try
                {
                    var context = services.GetRequiredService <ApplicationDbContext>();
                    ApplicationDbInitializer.Initialize(context);
                }
                catch (Exception ex)
                {
                    logger.Error(ex, "An error occurred while seeding the database.");
                }
            }

            host.Run();
        }
Exemplo n.º 26
0
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

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

                // Get our database context from the service provider
                var db = services.GetRequiredService <ApplicationDbContext>();

                // Get the UserManager and RoleManager also from the service provider
                var um = services.GetRequiredService <UserManager <ApplicationUser> >();
                var rm = services.GetRequiredService <RoleManager <IdentityRole> >();

                // Get the environment so we can check if this is running in development or otherwise
                var environment = services.GetService <IWebHostEnvironment>();

                // Initialise the database using the initializer from Data/ExampleDbInitializer.cs
                ApplicationDbInitializer.Initialize(db, um, rm, environment.IsDevelopment());
            }

            host.Run();
        }
Exemplo n.º 27
0
        public static void Main(string[] args)
        {
            //var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
            IWebHost host = null;

            try
            {
                //logger.Debug("Starting up...");
                host = BuildWebHost(args);
            }
            catch (Exception ex)
            {
                //logger.Error(ex, "Program could not start due to error.");
                throw;
            }
            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                var context  = services.GetRequiredService <ApplicationDbContext>();
                ApplicationDbInitializer.Initialize(context);
            }

            host.Run();
        }