Hangfire
Hangfire copied to clipboard
passing injected parameter
How can I inject instances in the method similar to CancellationToken ??
public class CustomService : ICustomService
{
public static ICustomService Null => null;
}
public interface ICustomService
{
}
public interface ICustomJob
{
void Execute(ICustomJob customToken);
}
public class CustomJob: ICustomJob
{
public void Execute(ICustomService customService)
{
Thread.Sleep(30000);
}
}
RecurringJob.AddOrUpdate<ICustomJob>(a => a.Execute(CustomService.Null), Cron.Minutely());
I agree that a way to accomplish this would be very useful. For example, it would be great to be able to modify filterContext.BackgroundJob.Job.Args (currently an IReadOnlyList<object>) in an IServerFilter's OnPerforming method.
good... thank you so much
I also need this. I wanted to create a server filter that instantiates and starts a profiler that will automatically be stopped in OnPerformed. However, I want to be able to pass the created profiler instance to the method body.
public sealed class ProfiledAttribute : JobFilterAttribute, IServerFilter
{
public void OnPerforming(PerformingContext context)
{
var profiler = new Profiler().Start();
// TODO: how to get this profiler inside the method body?
}
public void OnPerformed(PerformedContext context)
{
var profiler = context.BasckgroundJob.Job.Args[0] as IProfiler;
profiler.Stop();
}
}
BackgroundJob.Enqueue(() => DoMyJob(null));
[Profiled]
public void DoMyJob(IProfiler profiler)
{
// profiler is always null but I want it to be not null
}