hangfire使用
1 . NuGet 命令行执行
Install-Package Hangfire
2.首先在ConfigureServices 方法中注册服务:
services.AddHangfire(r=>r.UseSqlServerStorage("Data Source=.;Initial Catalog=HangfireDemo;User ID=sa;Password=123456"));
或者:
var connStr = Configuration[$"AppConnect:db"];
services.AddHangfire(x => x.UseSqlServerStorage(connStr));
3.然后在Configure 方法中加入HangfireServer及HangfireDashboard:
或者:
var jobOptions = new BackgroundJobServerOptions
{
Queues = new[] { "test_sync" },//队列名称,只能为小写
WorkerCount = Environment.ProcessorCount * 5, //并发任务数
ServerName = "TestService",//服务器名
};
app.UseHangfireServer(jobOptions);
app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
});
4. 启动://任务每分钟执行一次
RecurringJob.AddOrUpdate(() => Console.WriteLine($"ASP.NET Core LineZero"), Cron.Minutely()); return context.Response.WriteAsync("ok");
5.查看 http://localhost:/hangfire