Example #1
0
        public async static Task Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         .ReadFrom.Configuration(Configuration)
                         .Enrich.FromLogContext()
                         .CreateLogger();

            try
            {
                Log.Information("Host is starting...");

                var host = CreateHostBuilder(args).Build();

                await MigrationRepository.MigrateDatabase(host.Services);

                await MigrationRepository.SeedInitialData(host.Services);

                host.Run();

                Log.Information("Host is stopping...");
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Host terminated unexpectedly");
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }
        protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            builder.ConfigureServices(services =>
            {
                // Remove the app's ApplicationDbContext registration.
                var descriptor = services.SingleOrDefault(
                    d => d.ServiceType ==
                    typeof(DbContextOptions <ApplicationDbContext>));

                if (descriptor != null)
                {
                    services.Remove(descriptor);
                }

                // Add ApplicationDbContext using a test database for testing
                services.AddDbContext <ApplicationDbContext>(options =>
                                                             options.UseSqlServer(
                                                                 TestConstants.DbConnection,
                                                                 b => b.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName))
                                                             );

                services.AddAuthentication("Test")
                .AddScheme <AuthenticationSchemeOptions, TestAuthenticationHandler>(
                    "Test", options => { });

                // Build the service provider.
                var serviceProvider = services.BuildServiceProvider();

                // wait for migration data
                MigrationRepository.MigrateDatabase(serviceProvider).Wait();
                MigrationRepository.SeedInitialData(serviceProvider).Wait();

                // seed some sample data
                PrepareSampleData(serviceProvider);
            })
            .UseEnvironment("Test");
        }