问题描述
我想在ASP.NET Web API中创建一些功能,这些功能应每天在特定时间执行,并执行特定的任务,例如更新状态/记录/生成电子邮件,SMS.
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.
我应该在代码中创建TaskService吗?
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");
}
}
}
或者我应该在任务计划程序中创建.bat文件并创建新任务.
or should i create a .bat file and create a new task in Task Scheduler.
推荐答案
正如您在问题中提到的,您需要执行特定的任务,例如更新状态/记录/生成电子邮件,SMS等.
As you have mentioned in the question, you need to do the specific tasks like update statuses/Records/Generating Emails, SMS etc.
因此需要数据库访问权限,另一方面,您将必须发送电子邮件和SMS,这可能需要第三方库或其他配置设置访问权限.
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.
关于".bat文件和Windows计划程序",您需要具有使用有限的批处理命令来满足要求的出色技能.
About the ".bat file and windows scheduler", you need to have great skills using the limited batch commands available to fulfill your requirement.
所以,我的建议是代码,.exe和Windows Scheduler任务.
So, my suggestion is code, .exe and windows scheduler task.
此外,这应该是一个单独的应用程序,请勿将其与Web API代码混淆.您始终可以在带有Web API项目的Web API解决方案中创建一个新项目,并重复使用任何可能的代码.
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.
这篇关于WebApi中的Windows Task Scheduler或TaskService函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!