Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongo Migration dependency before Background service kicks off - .NET Core #80

Open
rajurh opened this issue Sep 5, 2023 · 1 comment

Comments

@rajurh
Copy link

rajurh commented Sep 5, 2023

Hello, I am working on a .NET 6 project that includes a background service. I have configured the migration in the startup as shown below. However, I have a problem: the background service tries to access the database before it is ready. This happens when the migration takes longer than usual to set up MongoDB. How can I ensure that the background service waits for the migration to finish before performing any database operations?

services
.AddMigration(new MongoMigrationSettings
{
Database = "blabla",
ConnectionString = config.GetConnectionString("MongoDb"),
DatabaseMigrationVersion = new DocumentVersion(1, 0, 0)
});

@Skuzzle-UK
Copy link

Create an extension
public static void UseMongoDbMigrations(this IApplicationBuilder builder)
{
try
{
using (var scope = builder.ApplicationServices.CreateScope())
{
var services = scope.ServiceProvider;
var migrationService = services.GetRequiredService();

        migrationService.Run();
    }
}
catch (Exception ex)
{
    // .... Some error handling ?
}

}

Then pop this in program.cs straight after the line var app = builder.Build();
app.UseMongoDbMigrations();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants