自己写一个类继承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>();
}