0

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.
     }
}
  • Did you mean `x.Run()` in the expression? The current code would not compile, unless you have omitted some code that would show otherwise. – Nkosi Jun 20 '19 at 12:11
  • 1
    Without a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) that clarifies your specific problem or additional details to highlight exactly what you need, it’s hard to tell exactly what you're asking. – Nkosi Jun 20 '19 at 12:21
  • What constructors does the `Check` class have? What is missing from it when hangfire runs the method? – playsted Jun 21 '19 at 18:41
  • Ive edited the post with more specific code example of the logic that Im trying to implement. – Dudu Butbul Jun 27 '19 at 06:32

0 Answers0