jamesmh / coravel

Near-zero config .NET library that makes advanced application features like Task Scheduling, Caching, Queuing, Event Broadcasting, and more a breeze!
https://docs.coravel.net/Installation/
MIT License
3.63k stars 243 forks source link

Using Dependency Injection with ScheduleAsync #362

Closed serajoddin-aliabadi closed 2 months ago

serajoddin-aliabadi commented 3 months ago

There is no example of using Dependency Injection in ScheduleAsync method within docs. I do it this way:

app.Services.UseScheduler(scheduler =>
{
    scheduler
        .ScheduleAsync(async () =>
        {
            await using var scope = app.Services.CreateAsyncScope();

            var job = scope.ServiceProvider.GetService<Coravel.Invocable.IInvocable>();

            await job.Invoke();
        })
        .Weekly()
        .RunOnceAtStart();
});

Is it right? or any other better way?

jamesmh commented 2 months ago

Yes, that's basically how you would do this 👍.

Using invocables is the recommended way to schedule tasks, but ultimately it's your choice 👍