0

I'm creating .net core 3.1 web api application. By default, it is configured to use IHostBuilder with Startup file which does some configurations

 public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });

I want to add some configurations into my Startup file (events producing) and the problem is that they will not be executed on the application startup but only when api receives the first request. That means that my hosted application won't be able to handle events before it gets a request. So the question is: How can I invoke configuration methods from the Startup file on the application start or how can I do specific configuration that will be executed on the application start?

Dmitry
  • 16,110
  • 4
  • 61
  • 73
Roman Borovets
  • 818
  • 12
  • 25
  • Why do you need to do this? The whole point of a web application is to process HTTP requests. If it's processing other things, it should not be a webapp. – Ian Kemp Mar 25 '20 at 11:54

1 Answers1

3

Use your Program.Main entry point.

Modify this autogenerated code:

public static void Main(string[] args)
{
  CreateHostBuilder(args).Build().Run();
}

To:

public static void Main(string[] args)
{
    var host = CreateHostBuilder(args).Build();

    // Use DI via host.Services:
    var myConfiguredService = host.Services.GetRequiredService<SomeService>();
    myConfiguredService.DoSomething();

    host.Run();
}

That was the preferred way to initialized DB with data in EF 2.0, and still works great for other purposes.

This (Main) method runs when dotnet run is executed, before it receives any requests.

But if you host webapp behind IIS be aware that IIS will not actually launch your app until it receives first request! So, you can't solve this problem "inside" your app, and I don't know if new/latest IIS has any settings about this.

Instead, you may use any uptime monitoring service and set it to check your webapp every 1-3-5 minutes, so your webapp will receive some "fake" request and spin up before actual "business" request arrives.

P.S. If you need async/await - change void Main to async Task Main and host.Run() to await host.RunAsync() (discussed here).

Dmitry
  • 16,110
  • 4
  • 61
  • 73
  • Thanks for the answer. But the problem is that the Main method does not get invoked either. Only when the first request comes... – Roman Borovets Mar 26 '20 at 10:03
  • Main method will be invoked when your app will launch/start (when `dotnet run` will be executed). If you host webapp, say, behind IIS and IIS does not start your app until first request received - this can't be solved "inside" your app. – Dmitry Mar 26 '20 at 10:47
  • Yes, this is exactly the problem! I've debugged it from VS with default run command. Thank you!. Include this into the answer and I'll accept it as a correct answer – Roman Borovets Mar 31 '20 at 12:19
  • You have multiple options to warm up iis to prepare the app beforehand: https://stackoverflow.com/a/59184181/3224794 – Владимiръ May 17 '21 at 21:49