日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

.Net 简单使用 Hangfire

發布時間:2024/1/8 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .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的全部內容,希望文章能夠幫你解決所遇到的問題。

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