Windows WebApi 中的任务计划程序或任务服务函数
Windows Task Scheduler OR TaskService Functions in WebApi
我想在 ASP.NET Web API 中创建一些功能,这些功能应该每天在特定时间执行并执行特定任务,例如更新 statuses/Records/Generating 电子邮件、短信。
我应该在代码中创建一个 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 文件并在任务计划程序中创建一个新任务。
我推荐你试试Hangfire. It's free and you can use it for free in commercial app. Ducumentation you can find here。
您应该在您的网络代码之外执行此操作。这是因为您的 Web 应用程序不应该访问任务系统或 Web 服务。默认情况下,IIS 7.5+ 在其自己的受限用户帐户 (https://www.iis.net/learn/manage/configuring-security/application-pool-identities) 中运行应用程序。
如果你想有一个可靠的任务调度,你可以根据你的选择应用时间间隔,我推荐 [quartz]:https://www.quartz-scheduler.net/。 Quartz 允许 add/edit/delete/etc 轻松、可管理且没有 CPU 开销的计划任务。
此外,Quartz 是一个开源作业调度系统,可用于从最小的应用程序到大型企业系统。
正如您在问题中提到的,您需要执行特定任务,例如更新 statuses/Records/Generating 电子邮件、短信等
因此数据库访问进入了场景,另一方面,您将不得不发送电子邮件和短信,这可能需要第三方库或其他配置设置访问权限。
因此,要做到这一切最好使用代码实现,通过它您可以很好地维护您的更改和需求。
关于“.bat 文件和 windows 调度程序”,您需要 使用有限的批处理命令来满足您的要求。
所以,我的建议是代码、.exe 和 windows 调度程序任务。
此外,这应该是一个单独的应用程序,不要将其与 Web API 代码混淆。您始终可以使用 Web API 项目在 Web API 解决方案中创建一个新项目,并重复使用任何可能的代码。
我想在 ASP.NET Web API 中创建一些功能,这些功能应该每天在特定时间执行并执行特定任务,例如更新 statuses/Records/Generating 电子邮件、短信。
我应该在代码中创建一个 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 文件并在任务计划程序中创建一个新任务。
我推荐你试试Hangfire. It's free and you can use it for free in commercial app. Ducumentation you can find here。
您应该在您的网络代码之外执行此操作。这是因为您的 Web 应用程序不应该访问任务系统或 Web 服务。默认情况下,IIS 7.5+ 在其自己的受限用户帐户 (https://www.iis.net/learn/manage/configuring-security/application-pool-identities) 中运行应用程序。
如果你想有一个可靠的任务调度,你可以根据你的选择应用时间间隔,我推荐 [quartz]:https://www.quartz-scheduler.net/。 Quartz 允许 add/edit/delete/etc 轻松、可管理且没有 CPU 开销的计划任务。 此外,Quartz 是一个开源作业调度系统,可用于从最小的应用程序到大型企业系统。
正如您在问题中提到的,您需要执行特定任务,例如更新 statuses/Records/Generating 电子邮件、短信等
因此数据库访问进入了场景,另一方面,您将不得不发送电子邮件和短信,这可能需要第三方库或其他配置设置访问权限。
因此,要做到这一切最好使用代码实现,通过它您可以很好地维护您的更改和需求。
关于“.bat 文件和 windows 调度程序”,您需要 使用有限的批处理命令来满足您的要求。
所以,我的建议是代码、.exe 和 windows 调度程序任务。
此外,这应该是一个单独的应用程序,不要将其与 Web API 代码混淆。您始终可以使用 Web API 项目在 Web API 解决方案中创建一个新项目,并重复使用任何可能的代码。