如何在ASP.NET Core项目启动时执行异步定时任务

背景介绍

  项目环境为ASP.NET Core 2.1.2。

  需要在项目启动时运行一个定时任务,在后台每隔一定时间执行任务。

实现方法

  1、写一个任务服务类继承BackgroundService

public class APIDataService : BackgroundService
    {
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    //需要执行的任务

                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex.Message);
                }
                await Task.Delay(1000, stoppingToken);//等待1秒
            }
        }
    }

 2、在Startup.cs中注入

public void ConfigureServices(IServiceCollection services)
{
       ......
  services.AddSingleton<Microsoft.Extensions.Hosting.IHostedService, APIDataService>();
}
锲而舍之,朽木不折;锲而不舍,金石可镂。
原文地址:https://www.cnblogs.com/zhengyb/p/14298300.html