Web任务计划程序或WebApi中的TaskService函数

Muh*_*ain 14 c# asp.net-web-api taskscheduler

我想在ASP.NET Web API中创建一些函数,这些函数应该在特定时间每天执行,并执行特定任务,如更新状态/记录/生成电子邮件,SMS.

我应该在Code中创建TaskService

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");
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

或者我应该在任务计划程序中创建.bat文件并创建新任务.

Jos*_*cis 5

正如您在问题中提到的,您需要执行特定任务,如更新状态/记录/生成电子邮件,SMS等.

因此,数据库访问进入场景,另一方面,您将不得不发送可能需要第三方库或其他配置设置访问的电子邮件和SMS.

因此,要做到这一切,最好使用代码实现,通过它可以很好地维护您的更改和需求.

关于".bat文件和Windows调度程序",您需要使用可用的有限批处理命令来满足您的要求.

所以,我的建议是代码,.exe和Windows调度程序任务.

此外,这应该是一个单独的应用程序,不要与Web API代码混淆.您始终可以使用Web API项目在Web API解决方案中创建新项目,并重用任何可能的代码.