35

In PHP we have cron jobs, where the hosting server automatically picks up and executes a task as per the schedule given.

What would be a good alternative to use for CRON jobs in ASP.NET? I'd like to use a Web service, but that will not work in a shared hosting environment. Any help would be appreciated, and please advise a way to do this specifically in a shared hosting environment.

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Farooq Ahmed Khan
  • 3,975
  • 3
  • 27
  • 36
  • Some shared hosting environments offer scheduled jobs. What hosting are you using? – agarcian Nov 22 '11 at 12:43
  • possible duplicate of [How to emulate Cron Jobs on a Windows Server?](http://stackoverflow.com/questions/850556/how-to-emulate-cron-jobs-on-a-windows-server) – Luke Girvin Nov 22 '11 at 12:44

5 Answers5

26

Try Quartz.NET. It's a decent .NET scheduler which supports CRON expressions, CRON triggers and various other means and methods to schedule tasks to be performed at certain times / intervals.

It even includes a basic Quartz.NET server (Windows Application) that might fit your needs.

Edit:

If you can't run applications or windows services within your shared hosting then perhaps something like "Easy Background Tasks in ASP.NET" will do you? It simulates a Windows Service using HttpRuntime.Cache and removes the need for any external dependancies.

Community
  • 1
  • 1
Johannes Kommer
  • 6,401
  • 1
  • 39
  • 45
  • 2
    isn't there a simple method without including any product in my project. ? – Farooq Ahmed Khan Nov 22 '11 at 12:57
  • 1
    @FarooqAhmed The Windows Scheduler, although it is [not available](http://community.godaddy.com/groups/web-hosting/forum/topic/schedule-tasks-on-windows-host/) within GoDaddy's shared hosting and still requires a point to hook into your application (i.e. a page it can hit) – Johannes Kommer Nov 22 '11 at 12:59
  • @FarooqAhmed see my update, this might work for you without an external dependency – Johannes Kommer Nov 22 '11 at 13:08
5

Another option is to use hangfire which says about itself

An easy way to perform background processing in .NET and .NET Core applications. No Windows Service or separate process required.

Backed by persistent storage. Open and free for commercial use.

Requirements (as of November 2019) are

  • .NET Framework 4.5
  • Persistent storage, e.g. an SQL database where Hangfire will write 10 tables
  • Newtonsoft.Json library ≥ 5.0.1

Assuming that you have already some API project which runs as service, you can use hangfire within.

Disclaimer: I am not affiliated in any kind with hangfire, I will just be using it soon.

Community
  • 1
  • 1
B--rian
  • 5,578
  • 10
  • 38
  • 89
3

nCron is a .Net implementation of CRON using nCronTabs which are the exact same as cron scheduling

2

Asp.Net and Web services are typically responsive. They wait and respond to requests. There are hacks to make them look like schedulers but the hacks are ugly beyond belief.

You could use a Windows Service. Or write an executable and use the Windows Task Scheduler.

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
  • 1
    I have Go daddy hosting , Shared environment so i can't use this windows task scheduler i guess :S – Farooq Ahmed Khan Nov 22 '11 at 13:03
  • Just in case someone finds this like I did, Godaddy has a scheduled tasks feature that works like Cron jobs https://www.godaddy.com/help/scheduling-tasks-with-plesk-shared-hosting-8970 – Obi Jul 28 '15 at 14:35
1

This is absolutely a hack, but you can simulate a cron service using ASP.NET callbacks. Here is an example of how to do it.

Cotega
  • 339
  • 1
  • 2
  • 8
  • Please can you share with us what it is - if anything - that makes this 'hack' something to avoid? Thanks. – Oli Jun 12 '19 at 12:12
  • Links tend to go 404. Would you consider updating your answer to include the relevant information and/or code? – Doug Wilson Apr 18 '23 at 18:18