コード例 #1
0
        protected TestServer CreateServer()
        {
            var webHostBuilder = new WebHostBuilder()
                                 .UseStartup(typeof(Startup))
                                 .UseKestrel()
                                 .UseConfiguration(GetConfiguration())
                                 .ConfigureAppConfiguration((builderContext, config) =>
            {
                config
                .AddInMemoryCollection(new Dictionary <string, string>
                {
                    { "isTest", "true" }
                });
            });

            var testServer = new TestServer(webHostBuilder);

            var services = (IServiceScopeFactory)testServer.Host.Services.GetService(typeof(IServiceScopeFactory));

            using (var scope = services.CreateScope())
            {
                var context = scope.ServiceProvider.GetRequiredService <AppDbContext>();

                AppInitializer.Seed(context);
            }

            return(testServer);
        }
コード例 #2
0
        public static void Main(string[] args)
        {
            // Setting NLog, see nlog.config for the output configurations
            var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();

            try
            {
                logger.Debug("Init Main Program");
                var host = BuildWebHost(args);
                using (var scope = host.Services.CreateScope())
                {
                    var services = scope.ServiceProvider;

                    try
                    {
                        // Set the databas
                        var            context     = services.GetRequiredService <ApplicationDbContext>();
                        AppInitializer initializer = new AppInitializer(services, context, logger);
                        initializer.Seed().Wait();
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex, "An error occurred while seeding the database.");
                    }
                    host.Run();
                }
            }
            catch (Exception e)
            {
                //NLog: catch setup errors
                logger.Error(e, "Stopped program because of exception");
                throw;
            }
        }
コード例 #3
0
        public static void Main(string[] args)
        {
            // Setting NLog, see nlog.config for the output configurations
            var logger = LogManager.LoadConfiguration(Path.GetFullPath("nlog.config")).GetCurrentClassLogger();

            try
            {
                logger.Info("Init Main Program.");
                var host = BuildWebHost(args);
                using (var scope = host.Services.CreateScope())
                {
                    var services = scope.ServiceProvider;

                    try
                    {
                        // Set the databas
                        var context     = services.GetRequiredService <ApplicationDbContext>();
                        var initializer = new AppInitializer(services, context);
                        initializer.Seed().Wait();
                    }
                    catch (Exception ex)
                    {
                        logger.Error($"An error occurred while seeding the database. The message is {ex.Message}");
                    }

                    host.Run();
                }
            }
            catch (Exception e)
            {
                logger.Error($"Stopped program because of {e.Message}");
                throw;
            }
        }
コード例 #4
0
        public void SeedDatabase()
        {
            var services = (IServiceScopeFactory)Host.Services.GetService(typeof(IServiceScopeFactory));

            using (var scope = services.CreateScope())
            {
                var context = scope.ServiceProvider.GetRequiredService <AppDbContext>();

                AppInitializer.Seed(context);
            }
        }
コード例 #5
0
ファイル: ScenarioBase.cs プロジェクト: yfr7/CodeWithQB
        protected TestServer CreateServer()
        {
            var webHostBuilder = new WebHostBuilder()
                                 .UseStartup(typeof(Startup))
                                 .UseKestrel()
                                 .UseConfiguration(GetConfiguration())
                                 .ConfigureAppConfiguration((builderContext, config) =>
            {
                config
                .AddInMemoryCollection(new Dictionary <string, string>
                {
                    { "isTest", "true" }
                });
            });

            var testServer = new TestServer(webHostBuilder);

            var services = (IServiceScopeFactory)testServer.Host.Services.GetService(typeof(IServiceScopeFactory));

            using (var scope = services.CreateScope())
            {
                var context  = scope.ServiceProvider.GetRequiredService <AppDbContext>();
                var dateTime = scope.ServiceProvider.GetRequiredService <IDateTime>();

                try
                {
                    context.Database.EnsureDeleted();
                    context.Database.Migrate();
                    context.Database.EnsureCreated();
                }
                catch { }

                var eventStore = scope.ServiceProvider.GetRequiredService <IEventStore>();
                var repository = scope.ServiceProvider.GetRequiredService <IRepository>();
                AppInitializer.Seed(dateTime, eventStore, services, repository);
            }

            return(testServer);
        }