Cant use properties of instance while running job on Hangfire

172 Views Asked by At

I'm trying to use Hangfire as job scheduler.

I created class called Check that has some properties (non-static variables) and Run method.

when I trigger the Run method (using Hangfire framework) for specific instance of the class, the properties in the Run method are not initialized.

I understand that it is a behavior of the default JobActivator of Hangfire (when the Run method triggered, is create a new instance of Check and run the method using it).

For my understanding the solution is IoC Containers to force Hangfire use parameterized ctor. I tried to use Autofac but I am unable to get it work.

How can I send the parameters to the ctor while schedule the job?

example:

builder.RegisterType<Check>.AsSelf();
.
.
Check check = New Check(<Some Parameters for ctor>);
RecurringJob.AddOrUpdate<Check>("id", x => check.Run(), Cron.yearly);
.
.
.
class Check
{
     public int x, y, z; // for example

     public Check(int x, int y, int z) // ctor with parameters

     public Run()
     {
         // Here I'm trying to access properties of the instance
         // Like this.x but none of the them is initialized.
     }
}
0

There are 0 best solutions below