C#---使用Coravel实现定时任务

Coravel是一款框架轻,使用简单,支持秒级定时任务。

1.添加NuGet引用

2.定义自己的工作任务

cs 复制代码
using Coravel.Invocable;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CoravelDemo
{
    public class MyCoravelJob1 : IInvocable
    {
        private readonly ILogger _logger;

        public MyCoravelJob1(ILogger<MyCoravelJob1> logger)
        {
            _logger = logger;
        }

        public async Task Invoke()
        {
            _logger.LogInformation($"Coravel执行了一次{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
        }
    }
}

3.在Startup.cs中注册与配置自己的工作任务

cs 复制代码
using Coravel;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CoravelDemo
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            //只使用Coravel的任务调度功能
            services.AddScheduler();
            //注册自己的调度任务
            services.AddTransient<MyCoravelJob1>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            //配置任务
            var provider = app.ApplicationServices;
            provider.UseScheduler(scheduler =>
            {
                scheduler.Schedule<MyCoravelJob1>()
                //工作日每隔10秒输出
                .EverySeconds(10);
                //只在工作日
                //.Weekday();
            });
            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });
        }
    }
}

执行结果:

Coravel的主要方法:

|-------------------------|----------------------|
| EverySecond() | 每秒运行一次任务 |
| EveryFiveSeconds() | 每五秒运行一次任务 |
| EveryTenSeconds() | 每十秒运行一次任务 |
| EveryFifteenSeconds() | 每十五秒运行一次任务 |
| EveryThirtySeconds() | 每三十秒运行一次任务 |
| EverySeconds(3) | 每 3 秒运行一次任务。 |
| EveryMinute() | 每分钟运行一次任务 |
| EveryFiveMinutes() | 每五分钟运行一次任务 |
| EveryTenMinutes() | 每十分钟运行一次任务 |
| EveryFifteenMinutes() | 每十五分钟运行一次任务 |
| EveryThirtyMinutes() | 每三十分钟运行一次任务 |
| Hourly() | 每小时运行一次任务 |
| HourlyAt(12) | 在每小时过去 12 分钟运行任务 |
| Daily() | 每天午夜运行一次任务 |
| DailyAtHour(13) | 每天下午 1 点 UTC 运行一次任务 |
| DailyAt(13, 30) | 每天下午 1:30 UTC 运行一次任务 |
| Weekly() | 每周运行一次任务 |
| Cron("* * * * *") | 使用 Cron 表达式运行任务 |

相关推荐
极小狐3 分钟前
极狐GitLab 议题权重有什么作用?
开发语言·数据库·chrome·c#·gitlab
董先生_ad986ad3 小时前
C# 中的 `lock` 关键字本质
开发语言·c#
爱编程的鱼7 小时前
C# 枚举(Enum)声明与使用详解
java·windows·c#
冰茶_9 小时前
C#中常见的设计模式
java·开发语言·microsoft·设计模式·微软·c#·命令模式
chegan10 小时前
用c#从头写一个AI agent,实现企业内部自然语言数据统计分析(二)-数据结构和代码分析方法
ai·c#·agent
煤烦恼10 小时前
Kafka 命令行操作与 Spark-Streaming 核心编程总结
c#·linq
Zhen (Evan) Wang12 小时前
.NET 6 + Dapper + User-Defined Table Type
sqlserver·c#·.net·wpf
FAREWELL0007513 小时前
C#进阶学习(十四)反射的概念以及关键类Type
开发语言·学习·c#·反射·type
FAREWELL0007514 小时前
C#进阶学习(十三)C#中的预处理器指令
开发语言·学习·c#·预处理指令
qq_2979080114 小时前
c#简易超市充值卡程序充值消费查余额
经验分享·sqlserver·开源·c#·.net·开源软件