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

不想你离开。 提交于 2019-12-17 18:47:47

问题


I have method in MVC like

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

Can I schedule above method to execute for every 24 hours. I know we can set schedule with sql server agent and windows task scheduler . But due to some issues i want to execute current dll only, is this possible in MVC?


回答1:


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. 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.

To run something periodically without user interaction (that is, without a request to initiate it), a web application isn't what you want. 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.).

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.

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.




回答2:


Like everyone said that it is not a good practice to do long running background processes in your .net web application for reliability etc.

You can create an external trigger that calls into your MVC action method every 24 hours. So you are executing your code every 24 hours. 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. So in pingdom you can enter the url to your MVC action.

http://domain/controller/action

And that will be called every 24 hours and your code will run on schedule.

Update: recently i have been using Azure scheduler, which is build for scenarios like this. https://azure.microsoft.com/en-us/services/scheduler/




回答3:


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




回答4:


This sounds like an excellent use case for Revalee. Revalee is an open-source project that was developed to handle this type of situation. It manages the task persistence and scheduling using a Windows Service, but leverages a web application to handle the processing work. In your case, you would schedule your SendEmail action to be called once every 24 hours via the Service.

The following is the workflow of a web application that uses Revalee:


(source: sageanalytic.com)

Disclaimer: I was one of the developers involved with the Revalee project. To be clear, however, Revalee is free, open source software. The source code is available on GitHub.




回答5:


I agree with @David, running background tasks in web application is inherently not reliable, but there are scenarios, where this makes sense. 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/

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."

But again, it's not a good practice and if used, it should be used quite wisely.




回答6:


Here is an easy tutorial to send email from windows service. You can use the same database...etc. http://www.codeproject.com/Tips/704008/Send-Auto-Notification-Using-Windows-Service



来源:https://stackoverflow.com/questions/24536751/how-to-execute-a-method-in-asp-net-mvc-for-every-24-hours

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!