JonPSmith/RunStartupMethodsSequentially

How to wait for the execution

pharindoko opened this issue · 1 comments

Hey Jon,

I`m implemented your solution but my problem is that the start database migration should be done before the application will be started.
This is currently not the case.

This will be executed in Program.cs

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

string dbConnectionString = await GetConnectionString(builder);
SetupDbConnection(builder, dbConnectionString);

builder.Services.AddHttpContextAccessor();

// setup DI
builder.Services.AddScoped<IAccountInfo, AccountInfoAccessor>();
...
...
...
...
...


builder.Services.AddMicrosoftIdentityWebApiAuthentication(builder.Configuration, "AzureAd"); // uncomment for openid connect
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
SetupSwagger(builder);
builder.Services.AddCors();

builder.Services.AddMediatR(typeof(CommandBase).Assembly);

builder.Services.AddAuthorization(); // uncomment for open id and comment next

string lockFolder = builder.Environment.WebRootPath;
builder.Services.RegisterRunMethodsSequentially(options
    => options.AddPostgreSqlLockAndRunMethods(dbConnectionString)).RegisterServiceToRunInJob<MigrateDbContextService>();

WebApplication app = builder.Build();

...
...
...


Console.WriteLine("** Backend running and waiting for calls ... **");

app.Run();

And this is the output I get

** Backend running and waiting for calls ... **
Start database migration
Database migration finished

expected output

Start database migration
Database migration finished
** Backend running and waiting for calls ... **

Hi @pharindoko,

IHostedService, which the RunStartupMethodsSequentially library uses, is run before the web part is run. It's pretty complex but @andrewlock has provided a couple of articles about IHostedService - have a look at this section in a recent article.