日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

.Net 简单使用 Hangfire

發布時間:2024/1/8 asp.net 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .Net 简单使用 Hangfire 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡介

Hangfire 是一個開源框架,可幫助您創建、處理和管理后臺作業(官方文檔)

使用

  • 第一步:在Nuget上引用依賴Hangfire
  • 第二步:在Configuration注入Hangfire,Configure中使用面板
    在Configuration中使用AddHangfire和AddHangfireServer
public void ConfigureServices(IServiceCollection services) {services.AddControllersWithViews();services.AddHangfire(r => r.UseSqlServerStorage(@"Server=.;User ID=sa;Password=sa;database=AngelHangfire;"));services.AddHangfireServer(); }

在Configure中使用面板app.UseHangfireDashboard()

public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}else{app.UseExceptionHandler("/Home/Error");// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.app.UseHsts();}app.UseHttpsRedirection();app.UseStaticFiles();app.UseRouting();//app.UseHangfireServer(new BackgroundJobServerOptions());//棄用,改成在ConfigureServices中注入services.AddHangfireServerapp.UseHangfireDashboard();//使用面板app.UseAuthorization();app.UseEndpoints(endpoints =>{endpoints.MapControllerRoute(name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");}); }
  • 第三步:使用
//即發即棄的作業只執行一次,幾乎在創建后 立即執行 var jobId = BackgroundJob.Enqueue(() => Console.WriteLine("Fire-and-forget!"));//延遲任務執行 var jobId1 = BackgroundJob.Schedule(() => Console.WriteLine("Delayed!"),TimeSpan.FromDays(7));//循環任務執行,周期任務 RecurringJob.AddOrUpdate("AngelRecurringJob", () => Console.WriteLine("重復!"), Cron.Daily);
  • 第四步:打開面板
    hangfire面板是在網站地址后面加hangfire:https://localhost:44398/hangfire/

主頁

服務器

BackgroundJob.Enqueue 立即執行

BackgroundJob.Schedule 延遲任務執行

RecurringJob.AddOrUpdate 周期任務

總結

以上是生活随笔為你收集整理的.Net 简单使用 Hangfire的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。