Search code examples
c#asynchronousmaui

Run Async methods on MauiProgram.cs


I need to run some async methods that will intercat with db (create admin user, etc). I want to run them on MauiProgram.cs, wondering whats the best way of doing it.The code below works, not sure if its the best way of doing so.

  public static MauiAppBuilder SqlLiteInitializer(this MauiAppBuilder mauiAppBuilder)
  {
      var sqlLite = mauiAppBuilder.Services.BuildServiceProvider().GetRequiredService<ISqlLite<UserModel>>();

      Task.Run(() => sqlLite.CreateOrUpdateTableAsync()).Wait();

Solution

  • Well, async method returns already a task, so it's just overhead to wrap it in yet anowther, instead you can do:

    sqlLite.CreateOrUpdateTableAsync().Wait();