I am using Autofac as my Ioc container, here is my configuration code at startup.cs
IContainer container = AutofacConfig.Config();
Hangfire.GlobalConfiguration.Configuration
.UseSqlServerStorage(“medSTAR”)
.UseAutofacActivator(container);
app.UseHangfireDashboard(“/jobs”, new DashboardOptions()
{
Authorization = new { new HangFireAuthorizationFilter() }
});app.UseHangfireServer();
Here is my job process code.
public class BackgroundUpdater
{
protected readonly medSTARContainer _context;
public BackgroundUpdater(IMedStarObjectContext injectedContext)
{
_context = injectedContext as medSTARContainer;
}public void PopulateSingleEvaluationAssignment(int assignmentId) {//update evaluation assignments
_context.CallStoredProc(new StoredProc(@"Reports", @"PopulateSingleEvaluationAssignmentResponsesFlat"), new List<SqlParameter> { new SqlParameter("AssignnmentId", assignmentId) }); } }
The problem is, when I call the scheduled job, sometimes it succeeds after a couple of tries, when it fails, it is giving out an error message “No parameterless constructor defined for this object.” as if it is not recognizing the Autofac container, however, it will succeed sometimes, I am very confused with this inconsistent behavior.