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源码

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

相关推荐
記億揺晃着的那天8 小时前
Vue + Element UI 表格自适应高度如何做?
javascript·vue.js·ui
追逐时光者8 小时前
推荐 12 款开源美观、简单易用的 WPF UI 控件库,让 WPF 应用界面焕然一新!
后端·.net
真的想不出名儿9 小时前
Vue 中 props 传递数据的坑
前端·javascript·vue.js
Queen_sy9 小时前
vue3 el-date-picker 日期选择器校验规则-选择日期范围不能超过七天
javascript·vue.js·elementui
技术钱11 小时前
vue3 两份json数据对比不同的页面给于颜色标识
前端·vue.js·json
Flash Dog11 小时前
【Vue】——路由
vue.js
羊羊小栈13 小时前
基于「多模态大模型 + BGE向量检索增强RAG」的航空维修智能问答系统(vue+flask+AI算法)
vue.js·人工智能·python·语言模型·flask·毕业设计
喝拿铁写前端13 小时前
Vue 组件通信的两种世界观:`.sync` 与普通 `props` 到底有什么不同?
前端·vue.js·前端框架
用户221520442780013 小时前
vue3组件间的通讯方式
前端·vue.js
tyro曹仓舒14 小时前
Vue单文件组件到底需不需要写name
前端·vue.js