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>();

                    if (!context.Database.IsInMemory())
                    {
                        context.Database.Migrate();
                    }

                    // TODO, only run in debug mode
                    await ApplicationDbContextSeed.SeedTestData(context);
                }
                catch (Exception ex)
                {
                    var logger = scope.ServiceProvider.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred while migrating or seeding the database");
                }
            }

            host.Run();
        }
Exemple #2
0
        public static async Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                var config   = services.GetRequiredService <IConfiguration>();
                Log.Logger = new LoggerConfiguration()
                             .ReadFrom.Configuration(config)
                             .CreateLogger();

                var logger = services.GetRequiredService <ILogger <Program> >();
                var env    = services.GetService <IHostEnvironment>();
                logger.LogInformation("Starting LinkedOut in {Environment} mode", env.EnvironmentName);
                try
                {
                    // Attempt to migrate the database by first trying to get the SQL Server context. If it isn't found,
                    // we use GetRequiredService to fetch the PostgreSQL context, throwing an exception if it isn't available
                    SqlServerApplicationDbContext sqlServerContext = services.GetService <SqlServerApplicationDbContext>();
                    IApplicationDbContext         context;
                    if (sqlServerContext != null)
                    {
                        context = sqlServerContext;
                        ApplyMigration(logger, sqlServerContext);
                    }
                    else
                    {
                        var postgresqlContext = services.GetRequiredService <PostgreSqlApplicationDbContext>();
                        ApplyMigration(logger, postgresqlContext);
                        context = postgresqlContext;
                    }


                    if (env.IsDevelopment() || env.IsStaging())
                    {
                        // we only want to seed the database in staging or lower environments
                        logger.LogInformation("Performing database seed for {EnvironmentName}", env.EnvironmentName);
                        var testUserId = config.GetValue <string>("TestUserId", "");
                        if (string.IsNullOrWhiteSpace(testUserId))
                        {
                            testUserId = Guid.NewGuid().ToString();
                            logger.LogWarning("No test user id was supplied through a TestUserId configuration value. Using GUID {GUID} instead.", testUserId);
                        }

                        // this is a hack to ensure that our ICurrentUserService returns our seed value, instead of
                        // trying to call GetAuthenticationStateAsync before an AuthenticationState has been set

                        //OidcCurrentUserService oidcCurrentUserService = (OidcCurrentUserService)services.GetRequiredService<ICurrentUserService>();
                        //oidcCurrentUserService.UserIdOverride = testUserId;
                        await ApplicationDbContextSeed.SeedTestData(context, testUserId);

                        //oidcCurrentUserService.UserIdOverride = null;
                    }
                    else
                    {
                        logger.LogInformation("Skipping database seed for {EnvironmentName}", env.EnvironmentName);
                    }
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "An error occurred while migrating or seeding the database");
                }
            }

            try
            {
                Log.Logger.Information("Starting web host");
                host.Run();
            }
            catch (Exception ex)
            {
                Log.Logger.Fatal(ex, "Host terminated unexpectedly");
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }