自己寫一個類繼承BackgroundService
internal class RefreshService : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
//需要執行的任務
await Task.Delay(60000, stoppingToken);//等待60秒
}
}
}
Startup.cs中注入
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<Microsoft.Extensions.Hosting.IHostedService, RefreshService>();
}
