.Net Core 定时任务TimeJob

本文介绍了一个基于 .NET Core 的定时任务库 Pomelo.AspNetCore.TimedJob 的使用方法,包括安装、配置及创建定时任务的具体步骤。该库支持毫秒级定时任务、从数据库读取配置等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自:https://blog.youkuaiyun.com/u013711462/article/details/53449799

定时任务 Pomelo.AspNetCore.TimedJob

Pomelo.AspNetCore.TimedJob是一个.NET Core实现的定时任务job库,支持毫秒级定时任务、从数据库读取定时配置、同步异步定时任务等功能。

由.NET Core社区大神兼前微软MVP AmamiyaYuuko (入职微软之后就卸任MVP…)开发维护,不过好像没有开源,回头问下看看能不能开源掉。

作者自己的介绍文章: Timed Job – Pomelo扩展包系列

Startup.cs相关代码

我这边使用的话,首先肯定是先安装对应的包:Install-Package Pomelo.AspNetCore.TimedJob -Pre

然后在Startup.cs的ConfigureServices函数里面添加Service,在Configure函数里面Use一下。

// This method gets called by the runtime. Use this method to add services to the container.
publicvoidConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();
    //Add TimedJob services
    services.AddTimedJob();
}

 publicvoidConfigure(IApplicationBuilder app,
 IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    //使用TimedJob
    app.UseTimedJob();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseBrowserLink();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseStaticFiles();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
Job相关代码

接着新建一个类,明明为XXXJob.cs,引用命名空间using Pomelo.AspNetCore.TimedJob,XXXJob继承于Job,添加以下代码。

 public class AutoGetMovieListJob:Job
 {
     
     // Begin 起始时间;Interval执行时间间隔,单位是毫秒,建议使用以下格式,此处为3小时;
     //SkipWhileExecuting是否等待上一个执行完成,true为等待;
     [Invoke(Begin = "2016-11-29 22:10", Interval = 1000 * 3600*3, SkipWhileExecuting =true)]
     publicvoidRun()
     {
          //Job要执行的逻辑代码
          
         //LogHelper.Info("Start crawling");
         //AddToLatestMovieList(100);
         //AddToHotMovieList();
         //LogHelper.Info("Finish crawling");
     }
}

 

转载自:http://www.jkeabc.com/432165.html

转载于:https://www.cnblogs.com/ideacore/p/6297759.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值