我想在 ASP.NET Web API 中创建一些函数,这些函数应该每天在特定时间执行并执行特定任务,例如更新状态/记录/生成电子邮件、短信。
我应该在代码中创建一个 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");
}
}
}
或者我应该创建一个 .bat 文件并在任务计划程序中创建一个新任务。
正如您在问题中提到的,您需要执行特定任务,例如更新状态/记录/生成电子邮件、短信等。
因此,数据库访问进入了场景,另一方面,您将必须发送电子邮件和短信,这可能需要第三方库或其他配置设置访问。
因此,要做这一切结合代码实现会更好通过它你可以很好地维护你的变更和需求。
关于“.bat文件和Windows调度程序”,您需要有使用有限的批处理命令来满足您的要求的出色技能.
所以,我的建议是代码、.exe 和 Windows 调度程序任务。
另外,这应该是一个单独的应用程序,不要将其与 Web API 代码混淆。您始终可以使用 Web API 项目在 Web API 解决方案中创建一个新项目,并重用任何可能的代码。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)