I've installed and configured Hangfire in my .NET Core web application's Startup class as follows (with a lot of the non-Hangfire code removed):
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 IContainer getJobContainer()
{
var builder = new ContainerBuilder();
builder.RegisterModule(new BusinessBindingsModule());
builder.RegisterModule(new DataAccessBindingsModule());
return builder.Build();
}
}
As you can see, I'm using Autofac for DI. I've set things up to inject a new container each time the Hangfire job executes.
Currently, I have UseHangfireDashboard()
as well as the call to add my recurring job commented out and I'm receiving the following error on the line referencing IPrincipal
:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
I understand that Hangfire does not have an HttpContext
. I'm not really sure why it's even firing that line of code for the Hangfire thread. I'm ultimately going to need to resolve a service account for my IPrincipal dependency.
How can I address my issue with Hangfire and HttpContext?
Startup
? It needs an instance. Think about how Hangfire would have to run this scheduled task if you started the server withoutStartup
doing it, but you still have an expectation your previously registered task would run. – AkikoAddOrUpdate<IScheduledTaskService>(x => x.DailyTask(), Cron.Daily)
which will cause hangfire to resolve your service rather thanStartup
– AkikoIAppBuilder
is not part of Asp.Net-Core. Core usesIApplicationBuilder
was that a typo in the OP? – MoyerUseHangfireServer
, I then need to resolve HttpContext too. – CutlipIScheduledTaskManager
and its dependency onHttpContext
? You probably need to add theHttpContextAccessor
to the DI container. – MoyerHttpContext
-related code. It ends up throwing the error when I addUserHangfireServer
, not even when I try to add a job. – CutlipUseHangfireServer
. – Moyer