11

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Umer Waheed
  • 4,044
  • 7
  • 41
  • 62
  • Depends on your platform and deployment, but perhaps you can run the real cron (if Linux) or Windows Task Scheduler on the host? Both are proven and work. Otherwise you can always use timers or timer-based frameworks or put it in the database if you have one or... still, often the easiest solution is the best. – ewramner Apr 22 '20 at 18:14
  • Hosted service can be helpful: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-3.1&tabs=visual-studio – Mateech Apr 22 '20 at 18:21

4 Answers4

10

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."

Siavash Mortazavi
  • 1,382
  • 1
  • 14
  • 18
7

You can use Hangfire.

You can create recurring function with CRON pattern.

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

Documentation available here.

Ygalbel
  • 5,214
  • 1
  • 24
  • 32
1

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

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
0

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

        });
Karel Kral
  • 5,297
  • 6
  • 40
  • 50