If you familiar with production code you always encounter logic which needs to be invoked before anything should be requested/processed in your service.
I personally wrap it up into something like this and invoke it from DI framework:
public interface IMigrator
{
Task UpdateAsync();
}
For example:
- database migration from code
- static cache initializations
- seed apply to database
- authentication of root account,
- ...
I'm not so proficient with Web API framework at the moment and need to know, where should one put that logic? In Startup.Configure? Is it appropriate? How things turn out if it takes, say 10 minutes?
Need your advise:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services
.AddControllers();
services.Register(new CoreModule(), new DataModule());
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "api",
pattern: "api/v1/{controller}/{action}/{id?}");
});
//<- somewhere here? does it break some startup logic in IIS like winservices does when process takes longer than 1 minute?
await _migrator.UpdateAsync();
}
}
EDIT:
Found related article which describes how one should do initialization and where. And how to do this asynchronous - https://andrewlock.net/running-async-tasks-on-app-startup-in-asp-net-core-part-1/