I use Hangfire to perform a recurring job. I need to inject one dependency to the Hangfire task. I used Hangfire.Autofac to register my dependency
public class DiConfig
{
public static IContainer Register()
{
var container = new ContainerBuilder();
container.RegisterType<Logic>().As<ILogic>(); // I try different Life scope here
return container.Build();
}
}
This is my Startup class
public class Startup
{
public void Configuration(IAppBuilder app)
{
GlobalConfiguration.Configuration.UseSqlServerStorage("TssConnectionString");
var container = DiConfig.Register();
GlobalConfiguration.Configuration.UseAutofacActivator(container);
JobActivator.Current = new AutofacJobActivator(container);
var dashboardOptions = new DashboardOptions { AppPath = VirtualPathUtility.ToAbsolute("~/app/index.html#/") };
app.UseHangfireDashboard("/tasks", dashboardOptions);
BackgroundJob.Enqueue<DynamicQueueTask>( task => task.Execute());
}
}
and this is my Task class\
public class DynamicQueueTask
{
private readonly ILogic _logic;
public DynamicQueueTask(ILogic logic)
{
_logic= logic;
}
public void Execute()
{
//.....
_logic.DoSomeThing();
}
}
but i got No parameterless constructor defined for this … error
please advice me ?