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.

Muhammad Saqlain
  • 2,112
  • 4
  • 33
  • 48
  • 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. https://www.codeproject.com/Articles/6507/NET-Scheduled-Timer – Jon Feb 17 '17 at 20:21

4 Answers4

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.

Jose Francis
  • 950
  • 13
  • 28
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).

Andrew Monks
  • 656
  • 4
  • 11
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.

Rama Krishna
  • 101
  • 1
  • 1
  • Have you ever seen a working example of Quartz as of 2019 ? If so, please share. – elemer82 Aug 09 '19 at 15:35
  • 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? – elemer82 Aug 09 '19 at 15:36
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.

kmatyaszek
  • 19,016
  • 9
  • 60
  • 65
  • 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. – Andrew Monks Feb 09 '17 at 15:05
  • @AndrewMonks please read this article: http://docs.hangfire.io/en/latest/deployment-to-production/making-aspnet-app-always-running.html – kmatyaszek Feb 09 '17 at 15:06
  • Its even quite worrying that their "quick start" guide does not even consider any authentication on the dashboard! – Andrew Monks Feb 09 '17 at 15:08
  • @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 (http://docs.hangfire.io/en/latest/configuration/using-dashboard.html#configuring-authorization). – kmatyaszek Feb 09 '17 at 15:11
  • 1
    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. – Andrew Monks Feb 09 '17 at 15:11
  • I thought Api server should be stateless. Cause of background processes api could delay in response to clients. – Muhammad Saqlain Feb 10 '17 at 13:36
  • 2
    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](https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer) very simply to make a request to your web service to do the timing. – Eric Feb 15 '17 at 00:45