By Muhammad Saqlain


2017-02-09 14:53:03 8 Comments

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.

4 comments

@Jose Francis 2017-02-15 12:26:08

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.

@Rama Krishna 2017-02-15 08:36:24

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.

@elemer82 2019-08-09 15:35:02

Have you ever seen a working example of Quartz as of 2019 ? If so, please share.

@elemer82 2019-08-09 15:36:06

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?

@Andrew Monks 2017-02-09 15:03:08

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

@kmatyaszek 2017-02-09 15:03:03

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

@Andrew Monks 2017-02-09 15:05:23

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.

@kmatyaszek 2017-02-09 15:06:28

@AndrewMonks please read this article: docs.hangfire.io/en/latest/deployment-to-production/…

@Andrew Monks 2017-02-09 15:08:21

Its even quite worrying that their "quick start" guide does not even consider any authentication on the dashboard!

@kmatyaszek 2017-02-09 15:11:11

@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/…).

@Andrew Monks 2017-02-09 15:11:32

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.

@Muhammad Saqlain 2017-02-10 13:36:03

I thought Api server should be stateless. Cause of background processes api could delay in response to clients.

@Eric 2017-02-15 00:45:37

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.

Related Questions

Sponsored Content

2 Answered Questions

1 Answered Questions

1 Answered Questions

[SOLVED] Editing the existing task in the task scheduler C++

1 Answered Questions

How to save a new trigger on an existing Scheduled Task

  • 2018-01-16 06:38:51
  • Cameron Castillo
  • 376 View
  • 0 Score
  • 1 Answer
  • Tags:   c# taskservice

1 Answered Questions

[SOLVED] How to debug task scheduler on win 10?

1 Answered Questions

1 Answered Questions

[SOLVED] UnauthorizedException trying to use Windows TaskScheduler wrapper

3 Answered Questions

[SOLVED] Task Scheduling using C#

3 Answered Questions

[SOLVED] Task Scheduler using C# (To Schedule .bat(batch) file)

Sponsored Content