前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >.NET 6 + Hangfire 实现后台作业管理

.NET 6 + Hangfire 实现后台作业管理

原创
作者头像
软件工程师Michael
发布2022-12-10 18:08:26
1.4K0
发布2022-12-10 18:08:26
举报

一.环境:

ASP.NET Core 6 + Hangfire + MySQL

二、新建ASP.NET Core空项目

项目名称:HangfireExample

框架:.NET 6.0

三、Nuget引入程序集

Hangfire.Core

Hangfire.MySqlStorage                   --mysql数据库存储

Hangfire.AspNetCore                     --AspNetCore支持

Hangfire.Dashboard.BasicAuthorization   --可视化+权限控制

Hangfire.HttpJob                        --httpJob

创建MySQL数据库:hangfiredb

appsettings.json配置MySQL连接:

"ConnectionStrings": {
    "HangfireConnection": "server=192.168.5.234;Database=hangfiredb;userid=root;password=123456;SslMode=none;Allow User Variables=true;"
  },

Programe.cs程序:

using Hangfire;
using Hangfire.Dashboard.BasicAuthorization;
using Hangfire.HttpJob;
using Hangfire.MySql;
using System.Configuration;
using System.Transactions;
using static System.Net.WebRequestMethods;



var builder = WebApplication.CreateBuilder(args);
var Config = builder.Configuration;
// Add Hangfire services.
builder.Services.AddHangfire(configuration => configuration
    .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
    .UseSimpleAssemblyNameTypeSerializer()
    .UseRecommendedSerializerSettings()
    .UseStorage(new MySqlStorage(
        Config["ConnectionStrings:HangfireConnection"],
        new MySqlStorageOptions
        {
            TransactionIsolationLevel = IsolationLevel.ReadCommitted,
            QueuePollInterval = TimeSpan.FromSeconds(15),
            JobExpirationCheckInterval = TimeSpan.FromHours(1),
            CountersAggregateInterval = TimeSpan.FromMinutes(5),
            PrepareSchemaIfNecessary = true,
            DashboardJobListLimit = 50000,
            TransactionTimeout = TimeSpan.FromMinutes(1),
            TablesPrefix = "Hangfire"
        })).UseHangfireHttpJob());

// Add the processing server as IHostedService
builder.Services.AddHangfireServer();


var app = builder.Build();
app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
    Authorization = new[] {new BasicAuthAuthorizationFilter(new BasicAuthAuthorizationFilterOptions
    {
        RequireSsl = false,
        SslRedirect = false,
        LoginCaseSensitive = true,
        Users = new []
        {
            new BasicAuthAuthorizationUser
                    {
                        Login = "admin",
                        PasswordClear =  "admin"
                    }
        }
    })}
    
    app.UseRouting();
app.UseEndpoints(endpoints =>
{
    endpoints.MapGet("/", async context =>
    {
        await context.Response.WriteAsync("Hello World!");
    });
});

//app.MapGet("/", () => "Hello World!");

app.Run();

运行项目:

Hangfire运行结果
Hangfire运行结果

hangfire访问地址链接,输入账号admin,密码admin

https://localhost:5001/hangfire

Hangfire管理后台
Hangfire管理后台

自动创建了数据库表:

hangfiredb
hangfiredb

【小结】

Hangfire是当今最流行的任务调度框架之一,大型系统中常常会用到。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档