Example #1
0
        public static void Main(string[] args)
        {
            var host = BuildWebHost(args);

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

                try
                {
                    var dbContext = services.GetRequiredService <VaccinationDbContext>();
                    dbContext.Database.Migrate();
                    CatalogInitializer.InitializeCatalogs(dbContext);

                    var userManager = services.GetService <UserManager <IdentityUser> >();
                    var roleManager = services.GetService <RoleManager <IdentityRole> >();
                    StartupInitializer.Initialize(userManager, roleManager).Wait();
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred while migrating the database.");
                }
            }

            host.Run();
        }
        public async Task Ivoking_initializeasync_should_invoke_all_previously_added_initializers()
        {
            var firstInitializer   = new Mock <IStartupInitializer>();
            var secondInitializer  = new Mock <IStartupInitializer>();
            var startupInitializer = new StartupInitializer();

            startupInitializer.AddInitializer(firstInitializer.Object);
            startupInitializer.AddInitializer(secondInitializer.Object);
            await startupInitializer.InitializeAsync();

            firstInitializer.Verify(p => p.InitializeAsync(), Times.Once);
            secondInitializer.Verify(p => p.InitializeAsync(), Times.Once);
        }
Example #3
0
        public static async Task Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                try
                {
                    await Task.Run(() =>
                    {
                        var notificationContext = scope.ServiceProvider.GetRequiredService <GearNotificationsContext>();
                        if (notificationContext.Database.EnsureCreated())
                        {
                            notificationContext.Database.Migrate();
                        }
                    });

                    var context = await Task.Run(() => scope.ServiceProvider.GetService <IGearContext>());

                    var userManager = await Task.Run(() => scope.ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >());

                    var roleManager = await Task.Run(() => scope.ServiceProvider.GetRequiredService <RoleManager <ApplicationRole> >());

                    var concreteContext = (GearContext)context;
                    await Task.Run(() =>
                    {
                        if (concreteContext.Database.EnsureCreated())
                        {
                            concreteContext.Database.Migrate();
                        }
                        IndrivoInitializer.Initialize(concreteContext, userManager, roleManager);
                    });

                    var initializer = new StartupInitializer(roleManager, userManager);
                    await initializer.RoleInitialize(scope.ServiceProvider);

                    if (concreteContext.Database.EnsureCreated())
                    {
                        concreteContext.Database.Migrate();
                    }
                    await ReportsInitializer.Initialize(concreteContext, userManager);
                }
                catch (Exception e)
                {
                    var logger = scope.ServiceProvider.GetRequiredService <ILogger <Program> >();
                    logger.LogError(e.Message, "An error occurred while migrating or initializing the database.");
                }
            }

            host.Run();
        }
Example #4
0
        public static IServiceCollection AddInitializers(this IServiceCollection services, params Type[] initializers)
        => initializers == null
              ? services
              : services.AddTransient <IStartupInitializer, StartupInitializer>(c =>
        {
            var startupInitializer = new StartupInitializer();
            var validInitializers  = initializers.Where(t => typeof(IInitializer).IsAssignableFrom(t));
            foreach (var initializer in validInitializers)
            {
                startupInitializer.AddInitializer(c.GetService(initializer) as IInitializer);
            }

            return(startupInitializer);
        });