NETCORE - TimeJob定时任务的使用

NETCORE - TimeJob定时任务的使用

 

 

1. 安装 nuget 包

Install-Package Pomelo.AspNetCore.TimedJob -Pre

或 TimedJob 包

TimedJob

 

 

 

2. startup.cs 

Start.cs的ConfigureServices注入AddTimedJob服务

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddTimedJob();

        }

 

 

 

Start.cs的Configure引入UseTimedJob中间件

 

复制代码
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseTimedJob();

            app.UseHttpsRedirection();
            app.UseMvc();
        }
复制代码

 

 

3. 使用

新建 JobTest.cs 类 继承Job。

复制代码
using Pomelo.AspNetCore.TimedJob;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NETCORE.TimeJob.JobClass
{
    public class JobTest : Job
    {

        //Begin:开始时间   Interval:间隔(毫秒,建议写成1000*60*60 格式)  SkipWhileExecuting:是否等待上一个执行完成
        [Invoke(Begin = "2018-07-27 00:00", Interval = 1000 , SkipWhileExecuting = true)]
        public void TestFun()
        {
            Console.WriteLine("my test job ~!");
        }

    }
}
复制代码

 

 

完成!

 

 

引用:https://www.cnblogs.com/ideacore/p/6297759.html

 

posted @   无心々菜  阅读(1594)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示