Hangfire .NET任务调度框架实例

1、介绍

Hangfire是一个开源的.NET任务调度框架,提供了内置集成化的控制台(后台任务调度面板),可以直观明了的查看作业调度情况。

2、Nuget安装

3、编写代码

1)测试服务:FirstStartService

cs 复制代码
using Microsoft.Extensions.Hosting;
namespace HangfireTest.Service
{
    public class FirstStartService: IHostedService
    {
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            await Task.Run(() =>
            {
                Console.WriteLine("FirstStartService......StartAsync");
            }, cancellationToken);
            //hangfire定时任务
            var id = Hangfire.BackgroundJob.Enqueue(() => Console.WriteLine("插入队列的任务"));
            Hangfire.BackgroundJob.Schedule(() => Console.WriteLine("延迟的任务"), TimeSpan.FromSeconds(5));
            Hangfire.RecurringJob.AddOrUpdate(() => Console.WriteLine("循环执行的任务"), Hangfire.Cron.Minutely);
            Hangfire.BackgroundJob.ContinueJobWith(id, () => Console.WriteLine("指定任务执行之后执行的任务"));
        }
        public async Task StopAsync(CancellationToken cancellationToken)
        {
            await Task.Run(() =>
            {
                Console.WriteLine("FirstStartService......StopAsync");
            }, cancellationToken);
        }
    }
}

2) 测试服务:SecondStartService

cs 复制代码
using Microsoft.Extensions.Hosting;
namespace HangfireTest.Service
{
    public class SecondStartService: IHostedService
    {
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            await Task.Run(() =>
            {
                Console.WriteLine("SecondStartService......StartAsync");
            }, cancellationToken);
        }
        public async Task StopAsync(CancellationToken cancellationToken)
        {
            await Task.Run(() =>
            {
                Console.WriteLine("SecondStartService......StopAsync");
            }, cancellationToken);
        }
    }
}

3)Startup.cs注入测试服务和Hangfire(此实例项目为VS2022 .NET6所建的ASP.NET Core Web API测试项目 Program.cs和其他版本建的项目有所区别,但注入方式没区别)

cs 复制代码
/// <summary>
/// 应用程序的入口点和生命周期 -- 应用程序启动起处理的任务
/// </summary>
builder.Services.AddHostedService<FirstStartService>();
builder.Services.AddHostedService<SecondStartService>();

/// <summary>
/// Hangfire定时任务
/// </summary>
builder.Services.AddHangfire(x => x.UseSqlServerStorage("Data Source=localhost;Initial Catalog=Test;Integrated Security=True;")); // SqlServer
builder.Services.AddHangfireServer();

/// <summary>
/// Hangfire定时任务
/// </summary>
app.UseHangfireDashboard();

4、访问方式

访问调度控制面板:

本地访问方式:https://localhost:端口号/hangfire/

5、效果图

6、相关链接

① Hangfire官网

② Hangfire中文文档

③ Hangfire Github源码

一个简单的实例,如有帮助,欢迎点赞关注收藏!

相关推荐
a1117766 小时前
医院挂号预约系统(开源 Fastapi+vue2)
前端·vue.js·python·html5·fastapi
计算机毕设VX:Fegn08957 小时前
计算机毕业设计|基于springboot + vue蛋糕店管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
行走的陀螺仪8 小时前
uni-app + Vue3编辑页/新增页面给列表页传参
前端·vue.js·uni-app
-凌凌漆-11 小时前
【vue】选项式api与组合式api
前端·javascript·vue.js
BYSJMG12 小时前
计算机毕业设计选题推荐:基于大数据的肥胖风险分析与可视化系统详解
大数据·vue.js·数据挖掘·数据分析·课程设计
Crazy Struggle13 小时前
.NET 中如何快速实现 List 集合去重?
c#·.net
phltxy13 小时前
Vue3入门指南:从环境搭建到数据响应式,开启高效前端开发之旅
前端·javascript·vue.js
东东51615 小时前
校园求职招聘系统设计和实现 springboot +vue
java·vue.js·spring boot·求职招聘·毕设
白中白1213817 小时前
Vue系列-2
前端·javascript·vue.js
BYSJMG17 小时前
计算机毕设选题推荐:基于Hadoop的交通事故数据可视化分析系统
大数据·vue.js·hadoop·分布式·后端·信息可视化·课程设计