asp.net core 2.0 后台定时自动执行任务

自己写一个类继承BackgroundService

1
2
3
4
5
6
7
8
9
10
11
internal class RefreshService : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
              //需要执行的任务
              await Task.Delay(60000, stoppingToken);//等待60秒
        }
    }
}

  

Startup.cs中注入

1
2
3
4
public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<Microsoft.Extensions.Hosting.IHostedService, RefreshService>();
}

  

 

转自:https://www.cnblogs.com/fireicesion/p/9867793.html

posted @ 2022-04-02 17:39  贾平凡  阅读(170)  评论(0)    收藏  举报