• NETCORE


    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

  • 相关阅读:
    Python 使用正则表达式匹配URL网址
    第3章 网络爬虫基础
    《精通Python网络爬虫》
    /etc/hosts
    Linux alias 命令
    file()
    Win10 取消桌面快捷键图标
    Win10 我的电脑 -- 右键点击管理打不开
    MongoDB 备份恢复
    ORACLE 日期比较
  • 原文地址:https://www.cnblogs.com/1285026182YUAN/p/12930687.html
Copyright © 2020-2023  润新知