I am having a ASPNet application and I have added a Task which is executing perfectly when I run the command
dotnet run --AppTasks=migrate.users --project
But I want to run that on my application startup.
What should I add in my configuration or program.cs so that it will run automatically?
App Tasks are specifically for running a task in the context of your Application then immediately existing. It’s not possible to run an AppTask with your App.
Instead you’ll just need to run whatever logic that’s in the AppTask, outside of the AppTask, e.g:
var migrator = new Migrator(
appHost.Resolve<IDbConnectionFactory>(),
typeof(Migration1000).Assembly);
migrator.Run();
@mythz I did write the code you suggested inside my Configure.Db which is outside the AppTask
It is hitting my debug points when I am starting the application but its not running any tasks for me.
Basically I have migrated my code to AspNetIdentity to use Integer Id and now trying to migrate my existing users inside UserAuth to the AspMembership tables. The task which I have written following the official post Migrating to ASP.NET Core Identity for Authentication - ServiceStack is perfect for me just I want to make sure that it runs when my application starts so I wont encounter any issues while going forward. (Just a context for what I am trying to do.)
The code isn’t run any tasks, it’s running the logic that was inside the migrate task.
The migration tasks runs any pending migrations that haven’t been run yet, if all migrations have been run then it wont have anything to run.
Running a migration when the App is run is highly unusual, typically you would want to run the migration task once then after the DB is migrated you wouldn’t need to run the same migration again.
Thats right and I am following that approach with Entity Frameworks migration which will compare the migration history table and only apply those which are pending and if none then it wont do anything.
I am a bit lost over here that I have created a Task to migrate user like follows in ConfigureDbMigrations.cs
AppTasks.Register("migrate.users", _ =>
{
var log = appHost.GetApplicationServices().GetRequiredService<ILogger<ConfigureDbMigrations>>();
log.LogInformation("Running migrate.users...");
var scopeFactory = appHost.GetApplicationServices().GetRequiredService<IServiceScopeFactory>();
using var scope = scopeFactory.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
using var db = scope.ServiceProvider.GetRequiredService<IDbConnectionFactory>().Open();
var migrateUsers = db.Select(db.From<UserAuth>().OrderBy(x => x.Id));
log.LogInformation("Migrating {Count} Existing ServiceStack Users to Identity Auth Users...", migrateUsers.Count);
MigrateExistingUsers(dbContext, scope.ServiceProvider, migrateUsers).Wait();
migrator.Run();
});
Now I want it to run at least once and then going onwards it can skip of course because why would we want to migrate the users again and again.
But just help me out over here do I need to add any migration for this or I have to run this task Manually? So that it will execute at leaste once. Lets assume I am starting from a fresh DB which do not have anything.