2

I have to give a call to Web API from that Helper method which will dump required Data to database.

I wanted to create a method which will be called automatically every hour.

Do i need to call it from Startup CS file if yes then how ?

The intended method will suppose to call API to get the data which will be dumped in SQL database in hourly manner.

S2K
  • 1,185
  • 3
  • 27
  • 55
  • 1
    ASP.NET Core (and everything that runs off a web server, really) is not suited for long running tasks or schedules. Look at simpler windows services and or the Task Scheduler for this – Camilo Terevinto Aug 12 '18 at 17:00
  • Have a read on IHostedService, https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-2.1 – Tseng Aug 12 '18 at 17:21
  • If you want some kind of data updated every hour, make the database do it. If you told us about your database, and what you want updated, we may be able to help better. – Dour High Arch Aug 12 '18 at 17:39
  • 1
    Hangfire is a great framework for scheduling tasks https://www.hangfire.io/ – Marcus Höglund Aug 12 '18 at 18:35
  • @DourHighArch updated question – S2K Aug 13 '18 at 14:08
  • @MarcusHöglund not able to call class action using hangfire : RecurringJob.AddOrUpdate(() => new ScopedCountryProcessingService().DoWork(this.LookupSearch), Cron.Minutely()); --- Do wotk method is not getting called – S2K Aug 13 '18 at 14:08
  • Its using IHosterService – S2K Aug 20 '18 at 10:28

4 Answers4

3

It depends on how you want to do it, and which OS you use.
If you use Linux, you can use the OS scheduler - cron. Or AnaCron.
If you you Windows, you could use Windows task scheduler.
Cron will probably work on MacOS too, though I suppose they have their own task-scheduler.
If you're using your application on Azure/AWS/DigitalOcean/AppEngine, then they all probably have facilities to do this.

There is a library to do job scheduling in .NET, it's called Quartz.
I don't know if they've ported to .NET Core yet, but I suppose they have.

Also, if you're using your application inside an ASP.NET (Core) application, I don't know how this is with IIS recycling the app-pool after 20 minutes of inactivity. If it does, then you can't do it in ASP.NET or you need to change the application's or IIS's configuration.
Note: According to this, the ASP.NET Core application definitely gets stopped when IIS recycles the app-pool, even though IIS would only have to recycle the reverse-proxy.

One of the ways we have done it in the past, is having an ashx handler in the application, which does the job once (using JWT header authentication so no DOS/DDOS is possible).

Then we just have a program running somewhere that issues a get request to the ashx handler every hour. This ensures the task is run every hour, and it lets IIS recycle the app-pool. Because that means IIS potentially needs to start then entire application, you should increase the get request's timeout accordingly. That system is simple, and it works the same everywhere, dev-machine, on-premise, self-hosted, shared-hosting, azure, aws, appengine, digitalocean, etc. and it doesn't even require any library.

Best of all, in its simplest form, you can just use wget and cron, which is really simple, and you can do rather complicated stuff with that, e.g run a program at the last day of the month (28,29,30,31) at 01:05, or like every 1st friday of the month, or on every monday-saturday from 4 to 23 o'clock, every 13 minutes starting 04:23, or whatever the hell you want.

ZERO .NET programming required at all for the job scheduling.

Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442
3

I would highly recommend Hangfire for this. https://www.hangfire.io/

jmdon
  • 997
  • 9
  • 17
  • @jmdn How to call a recurring method using hangfire, – S2K Aug 13 '18 at 14:04
  • @Soader03 yes i have cheked that but not able to call class action using hangfire : RecurringJob.AddOrUpdate(() => new ScopedCountryProcessingService().DoWork(this.LookupSearch), Cron.Minutely()); --- DoWork method is not getting called – S2K Aug 13 '18 at 14:09
  • If you have been through the tutorial and can't get it working then I suggest you open a new question, posting the relevant code relating to Hangfire. The issue can then be easier diagnosed. You need to go through the Installation and Configuration steps http://docs.hangfire.io/en/latest/installation.html – jmdon Aug 13 '18 at 14:53
1

You could set up a Web Hook in your application that call that method and create a Logic App in Azure that fires every hour to call that web hook.

Dara Oladapo
  • 586
  • 6
  • 12
0

It depends on a few things, notably where your application(s) are hosted ( azure, aws, on prem, locally) and how you want to couple your scheduler to the web api service. For example, say you are hosted in azure. You can use a timer in azure scheduled every hour to trigger and invoke your endpoint. AWS has a similar feature, and you can invoke a lambda function. If you are on prem, you could create a service such as hangfire that does the scheduling. And if your really looking to run the schedule manually, you could even create a WCF service with a Timer and TimerCallback that is invoked every hour.

Judy007
  • 5,484
  • 4
  • 46
  • 68
  • Application is hosted on AZURE, it gets data from third party web API so to call a web API from scheduled method, all the required dependencies must be resolved first – S2K Aug 13 '18 at 14:13