简体   繁体   中英

Implement recurring Jobs in HangFire using loc

I have my webApi that i would like to setup recurring jobs. Where about should i put the code? Also since i am using SimpleInjector, how can i get the service that i need to execute the method on?

Currently i have my extension method to setup HangFire like so

public static IApplicationBuilder UseHangFireServer(this IApplicationBuilder builder, Container container, BackgroundJobServerOptions options, JobStorage storage)
{
    if (builder == null) throw new ArgumentNullException("builder");
    if (options == null) throw new ArgumentNullException("options");
    if (storage == null) throw new ArgumentNullException("storage");

    var server = new BackgroundJobServer(options, storage);
    var lifetime = builder.ApplicationServices.GetRequiredService<IApplicationLifetime>();
    lifetime.ApplicationStopped.Register(server.Dispose);

    RecurringJob.AddOrUpdate("run-triggers", () => { "HERE I NEED A SERVICE, USING SIMPLE INJECTOR?" }, Cron.Hourly);

    return builder;
}

I have tried using this

RecurringJob.AddOrUpdate("run-triggers", () => container.GetInstance<IMyService>().Run(), Cron.Hourly);

But i get the error message saying

The IMyService is registered as 'Execution Context Scope' lifestyle, but the instance is requested outside the context of a Execution Context Scope.

I have also tried

RecurringJob.AddOrUpdate("trigger-queue", () => builder.ApplicationServices.GetService<IMyService>().Queue(), Cron.Hourly);

But this returns null.

It looks like you need to setup a new job activator for Hangfire so that it can supply the dependencies. (The default won't handle dependencies).

You could use the simple injector activator already written: https://www.nuget.org/packages/HangFire.SimpleInjector (or view the source and write your own)

Then you can view this to set it up (of set the activator property of the options instance you pass into the job server): http://docs.hangfire.io/en/latest/background-methods/using-ioc-containers.html

(not sure if these external links are ok, I feel like if they disappear from the internet, likely Hangfire would be defunct as well).

Now you can create a class for your recurring job, and Hangfire will use the activator to supply dependencies to your constructor.

IE:

class SomeJob {

    myDependency _param1;

    public SomeJob(myDependency param1) {
        _param1 = param1;
    }

    void ExecuteTask() {
        _param1.DoStuff();
    }
}

Then you can:

RecurringJob.AddOrUpdate((j) => j.Execute(), Cron.Hourly);

Hangfire will use the type parameter to create an instance of your class, using simple injector to supply the dependency to the ctor, then it will execute your job.

Hope that is useful

Steve

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM