我在.NET Core Web应用程序的Startup类中安装并配置了Hangfire,如下所示(删除了很多非Hangfire代码):
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseHangfireServer();
//app.UseHangfireDashboard();
//RecurringJob.AddOrUpdate(() => DailyJob(), Cron.Daily);
}
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<AppSettings>(Configuration);
services.AddSingleton<IConfiguration>(Configuration);
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<IPrincipal>((sp) => sp.GetService<IHttpContextAccessor>().HttpContext.User);
services.AddScoped<IScheduledTaskService, ScheduledTaskService>();
services.AddHangfire(x => x.UseSqlServerStorage(connectionString));
this.ApplicationContainer = getWebAppContainer(services);
return new AutofacServiceProvider(this.ApplicationContainer);
}
}
public interface IScheduledTaskService
{
void OverduePlasmidOrdersTask();
}
public class ScheduledTaskService : IScheduledTaskService
{
public void DailyJob()
{
var container = getJobContainer();
using (var scope = container.BeginLifetimeScope())
{
IScheduledTaskManager scheduledTaskManager = scope.Resolve<IScheduledTaskManager>();
scheduledTaskManager.ProcessDailyJob();
}
}
private …Run Code Online (Sandbox Code Playgroud) 我正在Hangfire BackgroundJob使用以下代码在 C# 中创建后台作业。
var options = new BackgroundJobServerOptions
{
ServerName = "Test Server",
SchedulePollingInterval = TimeSpan.FromSeconds(30),
Queues = new[] { "critical", "default", "low" },
Activator = new AutofacJobActivator(container),
};
var jobStorage = new MongoStorage("mongodb://localhost:*****", "TestDB", new MongoStorageOptions()
{
QueuePollInterval = TimeSpan.FromSeconds(30)
});
var _Server = new BackgroundJobServer(options, jobStorage);
Run Code Online (Sandbox Code Playgroud)
它创建 Jobserver 对象,然后,我创建计划、重复作业,如下所示。
var InitJob = BackgroundJob.Schedule<TestInitializationJob>(job => job.Execute(), TimeSpan.FromSeconds(5));
var secondJob = BackgroundJob.ContinueWith<Test_SecondJob>(InitJob, job => job.Execute());
BackgroundJob.ContinueWith<Third_Job>(secondJob, job => job.Execute());
RecurringJob.AddOrUpdate<RecurringJobInit>("test-recurring-job", job => job.Execute(), Cron.MinuteInterval(1));
Run Code Online (Sandbox Code Playgroud)
之后,我想在我的应用程序停止或关闭时删除或停止所有作业。因此,在我的应用程序的 OnStop 事件中,我编写了以下代码。 …
我刚刚开始使用 HangFire。当我从 IIS Express 本地运行它时,这些作业似乎工作正常,但是当我在我的开发服务器上运行它时,我收到没有定义无参数构造函数的错误。我之前使用的是 Simple Injector,但现在我已经转向 Autofac。
public class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
ConfigureIoc(app);
ConfigureRoutes();
ConfigureHangfire(app);
ScheduleXeroMonitor();
}
public void ConfigureHangfire(IAppBuilder app)
{
var authList = new List<IDashboardAuthorizationFilter>
{
new ClaimsBasedAuthorizationFilter("role", "admin")
};
var options = new DashboardOptions
{
Authorization = authList,
AppPath = "/Manage"
};
app.UseHangfireDashboard("/hangfire", options);
app.UseHangfireServer();
}
public void ConfigureRoutes()
{
BundleTable.EnableOptimizations = true;
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
public void ConfigureIoc(IAppBuilder app)
{
var builder = DependencyRegistrar.RegisterClasses();
var container = builder.Build(); …Run Code Online (Sandbox Code Playgroud)