Windows Task Scheduler OR TaskService Functions in WebApi
Asked Answered
K

4

14

I want to create some functions in ASP.NET Web API, which should be executed daily at specific time and do specific task like update statuses/Records/Generating Emails, SMS.

Should i create a TaskService in Code

using System;
using Microsoft.Win32.TaskScheduler;

class Program
{
   static void Main(string[] args)
   {
      // Get the service on the local machine
      using (TaskService ts = new TaskService())
      {
         // Create a new task definition and assign properties
         TaskDefinition td = ts.NewTask();
         td.RegistrationInfo.Description = "Does something";

         // Create a trigger that will fire the task at this time every other day
         td.Triggers.Add(new DailyTrigger { DaysInterval = 2 });

         // Create an action that will launch Notepad whenever the trigger fires
         td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));

         // Register the task in the root folder
         ts.RootFolder.RegisterTaskDefinition(@"Test", td);

         // Remove the task we just created
         ts.RootFolder.DeleteTask("Test");
      }
   }
}

or should i create a .bat file and create a new task in Task Scheduler.

Kamila answered 9/2, 2017 at 14:53 Comment(1)
I would suggest a Windows Service with a scheduled timer. I use this implementation in several of our production tasks. Let Windows handle failover, notifications, logging, restarting etc. And building a Windows Service in C# in Visual Studio is very easy using the template. codeproject.com/Articles/6507/NET-Scheduled-TimerSurfacetosurface
P
5

As you have mentioned in the question, you need to do the specific tasks like update statuses/Records/Generating Emails, SMS etc.

So database access comes into the scenario and on the other hand, you will have to send emails and SMS's which may require third party libraries or other configuration setting access.

Thus, to do all this it will be better to go with code implementation via which you can maintain your changes and requirements well enough.

About the ".bat file and windows scheduler", you need to have great skills using the limited batch commands available to fulfill your requirement.

So, my suggestion is code, .exe and windows scheduler task.

Also, this should be a separate application, don't mix it up with Web API code. You can always create a new project in the web API solution with web API project and reuse whatever code is possible.

Poteen answered 15/2, 2017 at 12:26 Comment(0)
A
1

You should do this outside your web code. This is because your webapp should have no access to the task system or web service. By default IIS 7.5+ runs app's in their own limited user account (https://www.iis.net/learn/manage/configuring-security/application-pool-identities).

Aveyron answered 9/2, 2017 at 15:3 Comment(0)
B
1

If you want to have a reliable tasks scheduling wherein you can apply time interval depend on your choice, I recommend [quartz]: https://www.quartz-scheduler.net/. Quartz allow to add/edit/delete/etc a scheduled task easily, manageable and no CPU overhead. Moreover Quartz is an open source job scheduling system that can be used from smallest apps to large scale enterprise systems.

Bespeak answered 15/2, 2017 at 8:36 Comment(2)
Have you ever seen a working example of Quartz as of 2019 ? If so, please share.Hamish
None of the examples for Quartz I tried are working. Also I would like to schedule Function(parameters) to run weekly, can you achieve that in Quartz?Hamish
W
0

I recommend you to try Hangfire. It's free and you can use it for free in commercial app. Ducumentation you can find here.

Wilds answered 9/2, 2017 at 15:3 Comment(7)
The only issue with this is without cleve / aedvanced config if the apppool gets shut down the task will not run. Plus might be overkill for just 1 task.Aveyron
@AndrewMonks please read this article: docs.hangfire.io/en/latest/deployment-to-production/…Wilds
Its even quite worrying that their "quick start" guide does not even consider any authentication on the dashboard!Aveyron
@AndrewMonks By default Hangfire allows access to Dashboard pages only for local requests. In order to give appropriate rights for production use, please see the Configuring Authorization section (docs.hangfire.io/en/latest/configuration/…).Wilds
I would not recommend making a webapp always run. I have done this before, but in a modern environment you don't always have this sort of control (Azure cloud, multi tenant host etc). It would be better practice to separate the scheduled tasks to another service.Aveyron
I thought Api server should be stateless. Cause of background processes api could delay in response to clients.Kamila
Running some code when the clock ticks is fundamentally wrong to do in a web service. A web service handles requests - that's what its good at. Keep it that way. The whole application model is bent for that. Create a distinct project to handle the clock ticking. You can use Azure Functions timer trigger very simply to make a request to your web service to do the timing.Millenarianism

© 2022 - 2024 — McMap. All rights reserved.