Hangfire icon indicating copy to clipboard operation
Hangfire copied to clipboard

passing injected parameter

Open dorbacao opened this issue 9 years ago • 3 comments

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());

dorbacao avatar Oct 14 '16 18:10 dorbacao

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.

turbodrubin avatar Oct 26 '16 23:10 turbodrubin

good... thank you so much

dorbacao avatar May 16 '17 20:05 dorbacao

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
}

RudeySH avatar Mar 25 '25 17:03 RudeySH