11

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?

László Koller
  • 1,139
  • 6
  • 15
Developer
  • 876
  • 5
  • 18
  • 44

6 Answers6

19

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.

David
  • 208,112
  • 36
  • 198
  • 279
  • 2
    +1, well explained. Maybe it's worth adding there are tons of ways to communicate between the ASP.Net application and the scheduled app/service (IPC): database, MSMQ...etc. – ken2k Jul 02 '14 at 16:57
8

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/

MoXplod
  • 3,813
  • 6
  • 36
  • 46
  • 1
    Why create hack-y solutions when there is no need to? What if pingdom goes down around the time the 24 hour trigger is set? – Shiva Jul 02 '14 at 18:07
  • 1
    haha, I did this exact same thing using nodeping.com, but I know about QueueBackgroundWorkItem and Hangfire... now... But this **hack** has been running for years every 3 minutes like a charm (99%+) for over 3 years now. Someday I will will make it right. And when it fails to complete in a timely fashion nodeping sends me a page and an email. :D Its just this stupendous "make it work in 10 minutes hack" that has simply never been revisited. – Hunter-Orionnoir Apr 29 '16 at 20:52
6
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:

Revalee Workflow
(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.

Community
  • 1
  • 1
László Koller
  • 1,139
  • 6
  • 15
  • DO wee need to install Revalee server in separate system OS and run as server? If so, we cannot host this type of services? – Developer Aug 01 '14 at 09:00
  • @sanjay: The Revalee Service can be installed on any Windows server, including the web hosting server. We've had cases where a user did not control the web server, but did control another server (like an email server) and, thus, was able to use Revalee on that 'other' server successfully. – László Koller Aug 01 '14 at 14:12
  • But we have other best choice, like Task scheduler in Windows with same functionality without any complex code, what is difference ? I need it should run in hosted website itself without any windows presence – Developer Aug 04 '14 at 06:42
1

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.

Robert Goldwein
  • 5,805
  • 6
  • 33
  • 38
  • 1
    These links talk about how to run a task, but not a *periodic* (i.e. once-every-24-hours) task. – ChrisW Nov 26 '17 at 21:51
0

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

Stefdelec
  • 2,711
  • 3
  • 33
  • 40