标签:Core app HangFire void services 定时 com public con
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 时间差不多
标签:Core,app,HangFire,void,services,定时,com,public,con 来源: https://www.cnblogs.com/ZkbFighting/p/11294467.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。