如何在特定时间调用 "Action Result" ASP.NET MVC(每日)

How to call "Action Result" ASP.NET MVC at specific time (Daily)

我有一个在特定时间向用户发送电子邮件的系统。 内置 ASP.NET MVC4 并有一个操作结果 "function" 用于检查消息的时间,如果消息的日期是今天则发送它。

如何以有效的方式调用此操作结果(每天) - 就像调度程序一样?

谢谢。

当您将新应用程序创建为 Windows 服务时,效率会更高。您可以在那里轻松设置代码以在特定时间启动。在此解决方案中,您将拥有更大的灵活性和独立性。你可以开始招聘了:Windows Service to run a function at specified time

虽然单独的服务/应用程序会更好,但您可以使用 wget

GNU Wget is a free software package for retrieving files using HTTP, HTTPS and FTP, the most widely-used Internet protocols. It is a non-interactive commandline tool, so it may easily be called from scripts, cron jobs, terminals without X-Windows support, etc.

然后你会做类似的事情:

"C:\Program Files (x86)\GnuWin32\bin\wget.exe" --no-check-certificate https://www.exammple.com/YouController/YourAction -O NUL

.bat 文件中,并在需要时通过 windows 计划任务将其设置为 运行(假设您不需要 运行 它少于每 60 秒 - 如果你这样做,请告诉我,因为我有另一种方法来解决这个问题,使用 windows 服务来调用 bat 文件。

省略 -O NUL 部分也会保存输出,因此您可以通过执行以下操作查看是否一切 运行 成功:

public ActionResult YourAction()
{
    //Do your code, get some stats that show it ran properly.
    return Content("Return your stats here.");
}

来自您的控制器操作。

您可以创建一个小型控制台应用程序,它只调用 API 发送电子邮件。然后,您可以使用 Windows 调度程序将控制台应用程序安排在特定时间 运行;您甚至可以 运行 而无需显示控制台 window。有关如何安排任务的详细信息,请参阅 here or here

使用 Azure Functions,这正是它的用途。真的很不错。

https://docs.microsoft.com/en-us/azure/azure-functions/functions-overview