'Where to add code in Asp.net after build and before Run of a server

I am creating a new service and want to call that before the server starts running but after all the builds are done (like connection to cosmos db and all)

I was thinking to add it in my program.cs, static void main. But I have something like this :

public static void main(string [] args)
{
  CreateBuilder(args).Build().Run();
}

public static IHostBuilder CreateBuilder (string[] args)

I saw IHostedService Interface and thought that might be a good place to add code after I build and before I run the servers. https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.ihostedservice?view=dotnet-plat-ext-6.0

Any idea ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source