简体   繁体   中英

Windows Task Scheduler OR TaskService Functions in WebApi

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.

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.

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM