Search code examples
c#asp.net-core.net-coreasp.net-core-3.1

ASP.NET Core - Where to perform an initialisation task before spawning background services?


I am learning ASP.NET Core and would like to perform a single asynchronous initialisation task before spawning background services. The initialisation task sets up kafka topics etc. based upon information in the appsettings.{Environment}.json config.

Is this best done in the Configure method of the Startup class? (See answer)


Solution

  • The task to setup the Kafka Topic is asynchronous and Startup.Configure is synchronous.

    I eventually made the decision to use Thomas Levesque's Extensions.Hosting.AsyncInitialization to perform the Kafka topic creation.

    For testing with xUnit and WebApplicationFactory I used a class fixture to perform the topic creation from test config before starting the WebApplicationFactory test server. See why here with a problem that I encountered with the test server blocking.