Core 定时任务之HangFire
生活随笔
收集整理的這篇文章主要介紹了
Core 定时任务之HangFire
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
ASP.NET Core 使用 Hangfire 很簡單,首先,Nuget 安裝程序包
> install-package Hangfire -pre然后ConfigureServices添加配置代碼:
public void ConfigureServices(IServiceCollection services){services.AddMvc();services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));//配置好數據庫鏈接后,會在數據庫對應的表中生成一些對應的表services.AddHangfire(x => x.UseSqlServerStorage("Data Source=LocalHost;Initial Catalog= Tab_Hangfire;Integrated Security=true;Persist Security Info=true"));//services.AddTimedJob();}然后Configure添加配置代碼:
public void Configure(IApplicationBuilder app, IHostingEnvironment env){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}app.UseHangfireServer();app.UseHangfireDashboard();//"0 3 * * *"//RecurringJob.AddOrUpdate(() => Console.WriteLine("Recurring!"), Cron.Minutely());//這里有幾個參數:<要執行的方法所在的類> http://localhost:52815/hangfire/周期性作業的編號名字 類的方法和參數 Corn表達式 RecurringJob.AddOrUpdate<InsertData>("Prozkb",p=>p.insert("zkb"), "0 */1 * * * ?", System.TimeZoneInfo.Local);
//這個Prozkb是RecurringJobId//app.UseTimedJob(); app.UseMvc();} public class InsertData{public void insert(string name){string sql = " insert into InsertData values('"+ name + "','',GETDATE())"; ExeNonQuery_B2B(sql);}public static void ExeNonQuery_B2B(string cmd){SqlConnection con = new SqlConnection();con.ConnectionString = "Data Source=LocalHost;Initial Catalog= Tab_Hangfire;Integrated Security=true;Persist Security Info=true";con.Open();SqlCommand com = new SqlCommand();com.Connection = con;com.CommandType = CommandType.Text;com.CommandText = cmd;SqlDataReader dr = com.ExecuteReader();dr.Close();con.Close();}}
http://localhost:52815/hangfire/? ? ? ? ? ?執行到數據庫差不多就是一分鐘左右
12:38:06.4170000? ? ?時間差不多
?
?
?
轉載于:https://www.cnblogs.com/ZkbFighting/p/11294467.html
總結
以上是生活随笔為你收集整理的Core 定时任务之HangFire的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第一章——数据结构之绪论
- 下一篇: Safe or Glitch-Free