.net core 基于Hangfire+Mysql持久化實(shí)現(xiàn)定時(shí)任務(wù)配置方法
1.negut引入hangfire相關(guān)包
Hangfire.AspNetCore,Hangfire.Core,Hangfire.Dashboard.BasicAuthorization,Hangfire.MySqlStorage

2.Appsetting 配置hangfire資源
"HangFire": {
"Connection": "Server=127.0.0.1;uid=root;pwd=wakamysql666;database=Hangfire_DB;AllowLoadLocalInfile=true;Allow User Variables=True;",
"pathMatch": "/hangfire",
"Login": "login",
"PasswordClear": "pwd"
},
3.自定義擴(kuò)展類(lèi)
/// <summary>
/// 任務(wù)調(diào)度
/// </summary>
public static class HangfireSetup
{
public static void AddHangfireSetup(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
if (services == null) throw new ArgumentNullException(nameof(services));
services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)//此方法 只初次創(chuàng)建數(shù)據(jù)庫(kù)使用即可
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseStorage(new MySqlStorage(Appsettings.app("HangFire", "Connection"), new MySqlStorageOptions
{
TransactionIsolationLevel =
(IsolationLevel?) System.Data.IsolationLevel.ReadCommitted, //事務(wù)隔離級(jí)別。默認(rèn)是讀取已提交
QueuePollInterval = TimeSpan.FromSeconds(15), //- 作業(yè)隊(duì)列輪詢(xún)間隔。默認(rèn)值為15秒。
JobExpirationCheckInterval = TimeSpan.FromHours(1),
CountersAggregateInterval = TimeSpan.FromMinutes(5),
PrepareSchemaIfNecessary = false, // 如果設(shè)置為true,則創(chuàng)建數(shù)據(jù)庫(kù)表。默認(rèn)是true
DashboardJobListLimit = 50000,
TransactionTimeout = TimeSpan.FromMinutes(1),
TablesPrefix = "Hangfire"
})));
services.AddHangfireServer();
}
}
4.在startupConfigureServices注入擴(kuò)展
services.AddHangfireSetup();//任務(wù)調(diào)度

5.配置MIddleware
//任務(wù)調(diào)度中間件
public static class HangfireMiddleware
{
public static void UseHangfireMiddleware(this IApplicationBuilder app)
{
if (app == null) throw new ArgumentNullException(nameof(app));
app.UseHangfireServer(); //配置服務(wù)//ConfigureOptions()
app.UseHangfireDashboard(Appsettings.app("HangFire", "pathMatch"), HfAuthor()); //配置面板
//BackgroundJob.Enqueue(() => Console.WriteLine("Hello world from Hangfire!"));
HangfireService(); //配置各個(gè)任務(wù)
}
/// <summary>
/// 配置賬號(hào)模板信息
/// </summary>
/// <returns></returns>
public static DashboardOptions HfAuthor()
{
var filter = new BasicAuthAuthorizationFilter(
new BasicAuthAuthorizationFilterOptions
{
SslRedirect = false,
RequireSsl = false,
LoginCaseSensitive = false,
Users = new[]
{
new BasicAuthAuthorizationUser
{
Login = Appsettings.app("HangFire", "Login"), //可視化的登陸賬號(hào)
PasswordClear = Appsettings.app("HangFire", "PasswordClear") //可視化的密碼
}
}
});
return new DashboardOptions
{
Authorization = new[] {filter}
};
}
/// <summary>
/// 配置啟動(dòng)
/// </summary>
/// <returns></returns>
public static BackgroundJobServerOptions ConfigureOptions()
{
return new()
{
Queues = new[] {"Job", nameof(HangfireConfigureQueue.picturetooss)}, //隊(duì)列名稱(chēng),只能為小寫(xiě)
WorkerCount = Environment.ProcessorCount * 5, //并發(fā)任務(wù)
ServerName = "HangfireServer" //代表服務(wù)名稱(chēng)
};
}
#region 配置服務(wù)
public static void HangfireService()
{
// "0 0 1 * * ? " 每天凌晨一點(diǎn)執(zhí)行阿里云OSS
RecurringJob.AddOrUpdate<IOrderItemInfoService>(_ => _.JobOSS(), "0 0 1 * * ? ", TimeZoneInfo.Local,
nameof(HangfireConfigureQueue.picturetooss));
// "0 0 1 * * ? " 每天早上七點(diǎn)執(zhí)行定時(shí)任務(wù)更新匯率
RecurringJob.AddOrUpdate<ICurrencyInfosService>(_ => _.UpdateRateByJob(), "0 0 7 * * ? ",
TimeZoneInfo.Local, nameof(HangfireConfigureQueue.picturetooss));
}
#endregion
}
6.startupConfigure配置使用中間件
app.UseHangfireMiddleware();//Job
效果圖:


結(jié)語(yǔ):到此hangfire實(shí)現(xiàn)定時(shí)任務(wù)的配置已經(jīng)全部完成。
到此這篇關(guān)于.net core 基于Hangfire+Mysql持久化實(shí)現(xiàn)定時(shí)任務(wù)的文章就介紹到這了,更多相關(guān).net core Hangfire定時(shí)任務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
aspx后臺(tái)傳遞Json到前臺(tái)的兩種接收方法推薦
下面小編就為大家?guī)?lái)一篇aspx后臺(tái)傳遞Json到前臺(tái)的兩種接收方法推薦。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05
.NET Core跨平臺(tái)執(zhí)行命令、腳本的方法詳細(xì)
這篇文章主要給大家介紹了關(guān)于.NET Core跨平臺(tái)執(zhí)行命令、腳本的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
使用ASP.NET一般處理程序或WebService返回JSON的實(shí)現(xiàn)代碼
今天, 將為大家說(shuō)明如何在 ASP.NET 中使用一般處理程序或者 WebService 向 javascript 返回 JSON2011-10-10
ASP.NET生成eurl.axd Http異常錯(cuò)誤的處理方法
在IIS6中同時(shí)啟用了ASP.NET 2.0 和 ASP.NET 4.0 后,網(wǎng)站程序可能會(huì)出現(xiàn)如下錯(cuò)誤:“ System.Web.HttpException: Path ‘//eurl.axd/‘ was not found. ”2011-05-05
asp.net及javascript判斷是否手機(jī)訪問(wèn)的方法
這篇文章主要介紹了asp.net及javascript判斷是否手機(jī)訪問(wèn)的方法,結(jié)合實(shí)例形式對(duì)比分析了asp.net及javascript實(shí)現(xiàn)判斷訪問(wèn)端類(lèi)型的相關(guān)技巧,需要的朋友可以參考下2016-06-06
VS2012下QT creator登錄對(duì)話框設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了VS2012下QT creator登錄對(duì)話框的設(shè)計(jì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
ADO.NET中的五個(gè)主要對(duì)象的詳細(xì)介紹與應(yīng)用
ADO.NET中的五個(gè)主要對(duì)象:Connection、Command、DataAdapter DataSet、DataReader詳細(xì)介紹與應(yīng)用,感興趣的朋友可以參考下2012-12-12
jquery中如何獲得服務(wù)器控件實(shí)現(xiàn)思路
jquery中如何獲得服務(wù)器控件,很多新手朋友對(duì)此比較陌生,接下來(lái)為您介紹解決方法,感興趣的朋友可以了解下哦2013-01-01

