我正在尝试使用Hangfire作为作业调度程序。
我创建了名为Check
的类,该类具有一些属性(非静态变量)和Run
方法。
当我为类的特定实例触发Run
方法(使用Hangfire框架)时,未初始化Run方法中的属性。
我知道这是Hangfire的默认JobActivator
的行为(当触发Run
方法时,将创建Check
的新实例并使用它来运行该方法)。
据我了解,解决方案是IoC容器强制Hangfire使用参数化的ctor。我尝试使用Autofac,但无法正常工作。
在计划作业时如何将参数发送到ctor?
示例:
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.
}
}