private static void TryInitFirstRun(IDbConnection dbConnection, ILogger logger) { var setupHelper = new SetupRunner(dbConnection); if (!setupHelper.TestDatabaseConnection(ex => { Trace.WriteLine(ex); Console.WriteLine(ex); })) { return; } if (!setupHelper.IsFirstRun()) { return; } try { logger.LogInformation("Initializing first run configuration..."); setupHelper.InitFirstRun(); logger.LogInformation("Database setup successfully."); } catch (Exception e) { logger.LogCritical(e, e.Message); } }
public void IsFirstRun_No() { _mockDbConnection.SetupDapper(c => c.ExecuteScalar <int>(It.IsAny <string>(), null, null, null, null)).Returns(1); var setupHelper = new SetupRunner(_mockDbConnection.Object); var result = setupHelper.IsFirstRun(); Assert.IsFalse(result); }
public async Task Invoke(HttpContext httpContext, IConfiguration configuration, IHostApplicationLifetime appLifetime, ILogger <FirstRunMiddleware> logger) { var initFlag = AppDomain.CurrentDomain.GetData(Token); if (null != initFlag) { // Don't need to check bool true or false, exists means everything await _next(httpContext); return; } var conn = configuration.GetConnectionString(Constants.DbConnectionName); var setupHelper = new SetupRunner(conn); if (!setupHelper.TestDatabaseConnection(exception => { logger.LogCritical(exception, $"Error {nameof(SetupRunner.TestDatabaseConnection)}, connection string: {conn}"); })) { httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError; await httpContext.Response.WriteAsync("Database connection failed. Please see error log, fix it and RESTART this application."); appLifetime.StopApplication(); } else { if (setupHelper.IsFirstRun()) { try { logger.LogInformation("Initializing first run configuration..."); setupHelper.InitFirstRun(); logger.LogInformation("Database setup successfully."); } catch (Exception e) { logger.LogCritical(e, e.Message); httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError; await httpContext.Response.WriteAsync("Error initializing first run, please check error log."); appLifetime.StopApplication(); } } AppDomain.CurrentDomain.SetData(Token, true); await _next(httpContext); } }
public static async Task InitStartUp(this WebApplication app) { using var scope = app.Services.CreateScope(); var services = scope.ServiceProvider; var env = services.GetRequiredService <IWebHostEnvironment>(); var dbConnection = services.GetRequiredService <IDbConnection>(); var setupHelper = new SetupRunner(dbConnection); try { if (!setupHelper.TestDatabaseConnection()) { return; } } catch (Exception e) { app.Logger.LogCritical(e, e.Message); return; } if (setupHelper.IsFirstRun()) { try { app.Logger.LogInformation("Initializing first run configuration..."); setupHelper.InitFirstRun(); app.Logger.LogInformation("Database setup successfully."); } catch (Exception e) { app.Logger.LogCritical(e, e.Message); } } try { var mediator = services.GetRequiredService <IMediator>(); var iconData = await mediator.Send(new GetAssetDataQuery(AssetId.SiteIconBase64)); MemoryStreamIconGenerator.GenerateIcons(iconData, env.WebRootPath, app.Logger); } catch (Exception e) { // Non critical error, just log, do not block application start app.Logger.LogError(e, e.Message); } }