You can't just change the signature, it needs to be void to be called by the framework.
Right now when you changed it to Task it means that the framework can't find it so it will not be called at all.
There's a GitHub issue regarding this here: https://github.com/dotnet/aspnetcore/issues/5897
It's quite tricky though...
There's no progress for 5.0 no, we don't know how to do this without blocking or breaking changes. It might be possible to run filters in 2 stages that never overlap.
Update based on your comment:
If you want to run something async during startup, I usually do like this:
I create a interface like this:
public interface IStartupTask
{
Task Execute();
}
Then a sample implementation like this
public class CreateDatabaseStartupTask : IStartupTask
{
public async Task Execute()
{
// My logic here
// await CreateDatabaseAsync();
}
}
Then in my Program.cs
public static async Task Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
// Resolve the StartupTasks from the ServiceProvider
var startupTasks = host.Services.GetServices<IStartupTask>();
// Run the StartupTasks
foreach(var startupTask in startupTasks)
{
await startupTask.Execute();
}
await host.RunAsync();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
And my Startup
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IStartupTask, CreateDatabaseStartupTask>();
}
}
So the important things are:
- Register your StartupTasks
- Build the host
- Resolve the StartupTasks
- Run the StartupTasks
- Start the Host