Async tasks in AfterInitCallbacks

Hello,

I need to start async tasks after AppHost is initialised.
For now my code is:

public override void Configure(Container container)
{
    ...
    var printerStatusChecker = new PrinterStatusChecker(dbFactory);
    container.Register(printerStatusChecker);    
    var itlcTcpClient = new ItlcTcpClient(dbFactory);
    container.Register(itlcTcpClient);

    AfterInitCallbacks.Add(async host =>
    {
         var printerStatusTask = printerStatusChecker.StartAsync();
         var itlcTcpClientTask = itlcTcpClient.StartAsync();
         await Task.WhenAll(printerStatusTask, itlcTcpClientTask);
    });
}

Both PrinterStatusChecker and ItlcTcpClient have while(true) { .. } loops inside StartAsync methods and will never finish executing non blocking code inside loop.
Everything seems to working fine, but do you see any problems or maybe better way to start these tasks?

Thank you,
Dejan

All Startup code in .NET Core or ASP.NET needs to be synchronous so your only option is to block in Startup code like AppHost.Configure().

In general async void should be avoided, at the very least you’ll want to wrap it in a try/catch to prevent any Exceptions taking down the process.