NETCORE - TimeJob定時任務的使用


NETCORE - TimeJob定時任務的使用

 

 

1. 安裝 nuget 包

Install-Package Pomelo.AspNetCore.TimedJob -Pre

 

 

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM