简体   繁体   English

如何每24小时在Asp.net MVC中执行一个方法

[英]How to execute a method in Asp.net MVC for every 24 hours

I have method in MVC like 我在MVC中有方法

 public void Sendmails()
 {
     //sending mails for every 24 hours.
 }

Can I schedule above method to execute for every 24 hours. 我可以安排上述方法每24小时执行一次。 I know we can set schedule with sql server agent and windows task scheduler . 我知道我们可以使用sql server agent和windows task scheduler设置日程安排。 But due to some issues i want to execute current dll only, is this possible in MVC? 但是由于某些问题我只想执行当前的dll,这在M​​VC中是否可行?

Possible? 可能? Perhaps, with a bit of hacking on invoking tasks and background threads. 也许,在调用任务和后台线程时有点黑客攻击。 Reliable? 可靠? Not in the slightest. 没有丝毫。

A web application is essentially a request-response system. Web应用程序本质上是一个请求 - 响应系统。 The application receives a request, performs its logic, and returns a response. 应用程序接收请求,执行其逻辑并返回响应。 Beyond that, it's idle. 除此之外,它是空闲的。 And it's at the mercy of the web server to be entirely unloaded from memory if the web server wants to use or free up that memory. 如果Web服务器想要使用或释放​​内存,那么Web服务器完全可以从内存中卸载。

To run something periodically without user interaction (that is, without a request to initiate it), a web application isn't what you want. 要在没有用户交互的情况下定期运行某些内容(即,没有启动它的请求),Web应用程序就不是您想要的。 Instead, you're looking for either a Windows Service or perhaps a simple Console Application scheduled to run at regular intervals by the host system's scheduling software (Windows Task Scheduler, cron, etc.). 相反,您正在寻找一个Windows服务或者一个简单的控制台应用程序,该应用程序计划由主机系统的调度软件(Windows任务计划程序,cron等)定期运行。

These applications can easily share code, of course. 当然,这些应用程序可以轻松共享代码。 They can be in the same solution and have references to the same class libraries. 它们可以位于同一个解决方案中,并且可以引用相同的类库。 Indeed, the application layer of your code should always be as thin and light as possible and all of the meaningful code should be in shared business logic assemblies. 实际上,代码的应用程序层应该始终尽可能轻薄,所有有意义的代码都应该在共享的业务逻辑程序集中。 Then any application (web application, windows service, console application, etc.) can simply reference those shared assemblies and invoke the same logic. 然后,任何应用程序(Web应用程序,Windows服务,控制台应用程序等)都可以简单地引用这些共享程序集并调用相同的逻辑。

Furthermore, multiple running applications can also easily share the same data. 此外,多个运行的应用程序也可以轻松共享相同的数据。 They can both connect to the same database (using shared data access logic, of course) and essentially be otherwise identical representations of centralized logic which happens to be invoked by separate application instances. 它们既可以连接到同一个数据库(当然也可以使用共享数据访问逻辑),而且基本上是集中逻辑的相同表示,恰好由不同的应用程序实例调用。

Like everyone said that it is not a good practice to do long running background processes in your .net web application for reliability etc. 就像每个人都说的那样,在.net Web应用程序中长时间运行后台进程以获得可靠性等不是一个好习惯。

You can create an external trigger that calls into your MVC action method every 24 hours . 您可以创建一个外部触发器,每24小时调用一次MVC操作方法 So you are executing your code every 24 hours. 因此,您每24小时执行一次代码。 There are multiple ways and services to do that, i have used different services. 有多种方法和服务可以做到这一点,我使用了不同的服务。 Or you can create your own if you do not want to use third party. 或者,如果您不想使用第三方,则可以创建自己的。

For example, you can use pingdom.com free account to call a certain url every <enter time here> period. 例如,您可以使用pingdom.com免费帐户在每次<enter time here>期间调用某个URL。 So in pingdom you can enter the url to your MVC action. 所以在pingdom中你可以输入你的MVC动作的URL。

http://domain/controller/action

And that will be called every 24 hours and your code will run on schedule. 这将每24小时调用一次,您的代码将按计划运行。

Update: recently i have been using Azure scheduler, which is build for scenarios like this. 更新:最近我一直在使用Azure调度程序,它是为这样的场景构建的。 https://azure.microsoft.com/en-us/services/scheduler/ https://azure.microsoft.com/en-us/services/scheduler/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace Proposal_Sln
{
public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        System.Timers.Timer timer = new System.Timers.Timer();
        timer.Interval = 1000;
        timer.Elapsed+=timer_Elapsed;
        timer.Start();
    }

    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        Models.Proposal_DBEntities DB = new Models.Proposal_DBEntities();
        Models.tbl_Year Y = new Models.tbl_Year();
        Y.YearName = "13" + DateTime.Now.Second.ToString();
        DB.tbl_Year.Add(Y);
        DB.SaveChanges();
    }
 }
}

its Work Good 它的工作很好

This sounds like an excellent use case for Revalee . 这听起来像Revalee的一个很好的用例。 Revalee is an open-source project that was developed to handle this type of situation. Revalee是一个开源项目,旨在处理这种情况。 It manages the task persistence and scheduling using a Windows Service, but leverages a web application to handle the processing work. 它使用Windows服务管理任务持久性和调度,但利用Web应用程序来处理处理工作。 In your case, you would schedule your SendEmail action to be called once every 24 hours via the Service. 在您的情况下,您将安排通过服务每24小时调用一次SendEmail操作。

The following is the workflow of a web application that uses Revalee: 以下是使用Revalee的Web应用程序的工作流程:

Revalee Workflow http://revalee.sageanalytic.com/Images/workflow.png Revalee Workflow http://revalee.sageanalytic.com/Images/workflow.png

Disclaimer: I was one of the developers involved with the Revalee project. 免责声明:我是参与Revalee项目的开发人员之一。 To be clear, however, Revalee is free, open source software. 但需要明确的是,Revalee是免费的开源软件。 The source code is available on GitHub . 源代码可在GitHub上获得

I agree with @David, running background tasks in web application is inherently not reliable, but there are scenarios, where this makes sense. 我同意@David,在Web应用程序中运行后台任务本质上是不可靠的,但有些场景,这是有道理的。 I'd recommend reading this excellent article first, http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx/ 我建议先阅读这篇优秀的文章, http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx/

With .NET 4.5.2, you can leverage HostingEnvironment, as nicely explains this article: http://blog.mariusschulz.com/2014/05/07/scheduling-background-jobs-from-an-asp-net-application-in-net-4-5-2 Quoting from that, "The HostingEnvironment.QueueBackgroundWorkItem method lets you schedule small background work items. ASP.NET tracks these items and prevents IIS from abruptly terminating the worker process until all background work items have completed." 使用.NET 4.5.2,您可以利用HostingEnvironment,正如本文所述: http ://blog.mariusschulz.com/2014/05/07/scheduling-background-jobs-from-an-asp-net-application- in-net-4-5-2从中引用,“HostingEnvironment.QueueBackgroundWorkItem方法允许您安排小的后台工作项.ASP.NET跟踪这些项目并阻止IIS突然终止工作进程,直到所有后台工作项都完成。 “

But again, it's not a good practice and if used, it should be used quite wisely. 但同样,这不是一个好的做法,如果使用,它应该非常明智地使用。

Here is an easy tutorial to send email from windows service. 这是一个从Windows服务发送电子邮件的简单教程。 You can use the same database...etc. 您可以使用相同的数据库...等。 http://www.codeproject.com/Tips/704008/Send-Auto-Notification-Using-Windows-Service http://www.codeproject.com/Tips/704008/Send-Auto-Notification-Using-Windows-Service

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM