Umer Waheed
Umer Waheed

Reputation: 4574

How to run cron job every day in asp. Net core application?

I have a web application written in asp. Net mvc core 2.2. O need to run a schedule job every day at 3:00 Am. What is the best way to do it?

I tried hangfire it stops after some time. We need to set IIS server always running. I googled and found hosted service in. Net core. Can anyone tell me what is the best approch to run a job daily in web application in dot net core?

Upvotes: 12

Views: 54422

Answers (4)

Karel Kral
Karel Kral

Reputation: 5487

There is a lightweight alternative to Quartz.Net: CronScheduler.AspNetCore.

I have used Quartz.Net in the past and for the current project, this was unnecessarily complex library. For simple use with no database try CronScheduler.AspNetCore.

   services.AddScheduler(ctx =>
    {
        ctx.AddJob<TestJob>();
    });

or the more complex registration:

services.AddScheduler(ctx =>
        {
            var jobName1 = "TestJob1";

            ctx.AddJob(
                sp =>
                {
                    var options = sp.GetRequiredService<IOptionsMonitor<SchedulerOptions>>().Get(jobName1);
                    return new TestJobDup(options, mockLoggerTestJob.Object);
                },
                options =>
                {
                    options.CronSchedule = "*/5 * * * * *";
                    options.RunImmediately = true;
                },
                jobName: jobName1);

        });

Upvotes: 1

Siavash Mortazavi
Siavash Mortazavi

Reputation: 1842

I've not personally tried it for .NET Core honestly, but have you tried Quartz Scheduler? https://www.quartz-scheduler.net/

[UPDATE]: According to Quartz GitHub repo: "Quartz.NET supports .NET Core/netstandard 2.0 and .NET Framework 4.6.1 and later."

Upvotes: 11

You can use EasyCronJob (disclaimer: I'm the author of this package)

services.ApplyResulation<TCronJob>(options =>{
 options.CronExpression = "* * * * *";
 options.TimeZoneInfo = TimeZoneInfo.Local;});

Documentation available here

Upvotes: 3

Ygalbel
Ygalbel

Reputation: 5519

You can use Hangfire.

You can create recurring function with CRON pattern.

RecurringJob.AddOrUpdate(() => Console.Write("Powerful!"), "0 12 * */2");

Documentation available here.

Upvotes: 7

Related Questions