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

#.NET Core #ASP.NET

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

  

评论 (0)

暂无评论,快来发表第一条评论吧!