MVC中集成Hangfire定时任务

本文介绍了一个开源.NET任务调度框架Hangfire的配置与使用方法。通过三个具体示例:发送带附件邮件、抓取代理IP并验证有效性,展示了Hangfire在实际项目中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

什么是HangfireHangfire 

一个开源的.NET任务调度框架,目前1.6+版本已支持.NET Core。个人认为它最大特点在于内置提供集成化的控制台,方便后台查看及监控,如下图:



Jucheap3.0中用到的技术

Hangfire

Hangfire.SqlServer

Hangfire.SimpleInjector

Hangfire.Console

在使用Hangfire的时候需要注意一点,Hangfire的Cron表达式暂时只支持到分钟级别的循环任务。

本实例中,主要用了3个定时任务来做demo

1.定时发送带附件的邮件

2.定时抓取指定几个网站的代理ip信息,并存放到本地数据库

3.定时验证抓取到的代理ip地址,是否有效


基本配置代码如下:

using System;
using System.Linq;
using System.Reflection;
using Hangfire;
using Hangfire.Console;
using Hangfire.SimpleInjector;
using Hangfire.SqlServer;
using JuCheap.Interfaces.Task;
using Owin;
using SimpleInjector.Extensions.ExecutionContextScoping;

namespace JuCheap.Web
{
    public partial class Startup
    {
        /// <summary>
        /// 配置Hangfire
        /// </summary>
        /// <param name="app"></param>
        public void ConfigureHangfire(IAppBuilder app)
        {
            JobIocConfig.Register();

            //Hangfire IOC配置
            GlobalConfiguration.Configuration.UseActivator(new SimpleInjectorJobActivator(JobIocConfig.Container));

            //hangfire 配置
            //使用SQLServer作为 数据库存储
            var storage = new SqlServerStorage("JuCheap-Job");
            GlobalConfiguration.Configuration.UseStorage(storage).UseConsole();

            GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = 0 });

            //OWIN-based authentication
            var options = new DashboardOptions
            {
                Authorization = new[]
                {
                    new HangfireAuthorizationFilter()
                }
            };
            app.UseHangfireDashboard("/taskcenter", options);

            LoadRecurringTasks();

            var jobOptions = new BackgroundJobServerOptions
            {
                ServerName = Environment.MachineName
            };
            app.UseHangfireServer(jobOptions);
        }

        /// <summary>
        /// 加载Job
        /// </summary>
        public static void LoadRecurringTasks()
        {
            using (JobIocConfig.Container.BeginExecutionContextScope())
            {
                var types = from type in Assembly.Load("JuCheap.Services").GetTypes()
                            where
                                type.Namespace?.StartsWith("JuCheap.Services.TaskServices") == true &&
                                type.GetInterfaces().Any(t => t == typeof(IRecurringTask))
                            select type;
                foreach (var type in types)
                {
                    var task = JobActivator.Current.ActivateJob(type) as IRecurringTask;
                    if (task == null)
                    {
                        continue;
                    }
                    if (task.Enable)
                    {
                        RecurringJob.AddOrUpdate(task.JobId, () => task.Excute(null), task.CronExpression, timeZone: TimeZoneInfo.Local);
                    }
                    else
                    {
                        RecurringJob.RemoveIfExists(task.JobId);
                    }
                }
            }
        }
    }
}



效果查看地址:http://www.jucheap.com/taskcenter

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值