asp.net core 2.0 定时任务

介绍TimeJob

首先项目添加引用

 

然后注册服务并使用:

services.AddTimedJob();//注册TimeJob服务

app.UseTimedJob();//使用timeJob

编写类,继承自Pomelo.AspNetCore.TimedJob.Job

  public class CountLogAndEmailJob:Job
    {
        private readonly IUserService _userSvc;
        private readonly IWorkLogService _logSvc;
        private readonly IEmailSender _emailSender;

        public CountLogAndEmailJob(IUserService userSvc, IWorkLogService logSvc, IEmailSender emailSender)
        {
            _logSvc = logSvc;
            _userSvc = userSvc;
            _emailSender = emailSender;
        }
        //Begin 起始时间;
        //Interval 表示时间间隔,单位是毫秒,此处事24小时;
        //SkipWhileExecuting 是否等待上一个执行任务完成,true为等待
          [Invoke(Begin = "2018-11-29 11:29",Interval = 1000*3600*24,SkipWhileExecuting = true)]
        public void Run(IServiceProvider services)
        {
            //定时任务具体执行的业务逻辑
            //查询


            //统计


            //发送邮件
            //_emailSender.SendEmailAsync("3xxxxxxx6@qq.com", "日志汇总情况", "有几个个人没写日志");
        }
    }

然后就OK啦。。。

posted @ 2018-11-29 11:40  程旭元Hany  阅读(802)  评论(1编辑  收藏  举报