1
votes

I am struggling to find the answer anywhere in documentation.

Where should a Hangfire recurring job be declared/called in an MVC application?

All the tutorials show just a one-liner declaration sample, which is plain obvious:

RecurringJob.AddOrUpdate(() => MethodToRun(42, "foo"), Cron.Minutely);

However, I am not sure where to insert that.

I need a service to work in background to check the status of some objects in DB and update the database with some changes.

There should be just one instance of this service, and it should run as soon as the application starts, so I was thinking about Startup.cs, right after I configure Hangfire?

Also, even though there should be one instance, the service should not be a static/singleton, because I will need to inject EF DbContext, so I expect I would run into issues with the context instance. I suppose Ninject dependency injection will handle this scenario just fine?

1

1 Answers

1
votes

Declaring the job in Startup.cs is fine. Using same context might create problems. So, its better to get a new instance of the context for the job. You can check the discussion thread at discuss.hangfire.io.