我正在尝试使用 Hangfire 作为作业调度程序。
我创建了Check
一个具有一些属性(非静态变量)和Run
方法的类。
当我Run
为类的特定实例触发方法(使用 Hangfire 框架)时,Run 方法中的属性未初始化。
我知道这是JobActivator
Hangfire 的默认行为(当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.
}
}