Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform periodic work on an ASP.NET MVC website?

Once a day, I want my ASP.NET MVC4 website, which may be running on multiple servers, to email a report to me. This seems like a pretty common thing to want to do, but I'm having a tough time coming up with a good way to do it.

Trying to run a timer on a server to do this work is problematic for a couple of reasons. If I have multiple servers then I'd have the timer running on all of them (in case a server goes down); I'd need to coordinate between them, which gets complicated. Also, trying to access the database via Entity Framework from a background thread adds the complication that I must employ a locking strategy to serialize construction/disposal of the DbContext object between the periodic background thread and the "foreground" Controller thread.

Another option would be to run a timer on the servers, but to have the timer thread perform a GET to a magic page that generates and emails the report. This solves the DbContext problem because the database accesses happen in a normal Controller action, serialized with all of the other Controller accesses to the database. But I'm still stuck with the problem of having potentially more than one timer running, so I'd need some smarts in the Controller action to ignore redundant report requests.

Any suggestions? How is this sort of thing normally done?

like image 538
Bob.at.Indigo.Health Avatar asked Apr 07 '13 05:04

Bob.at.Indigo.Health


People also ask

How do you execute a method in ASP.NET MVC for every 24 hours?

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.

What are the 3 main components of an ASP.NET MVC application?

The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller. The ASP.NET MVC framework provides an alternative to the ASP.NET Web Forms pattern for creating MVC-based Web applications.

What is the page lifecycle of an ASP.NET MVC?

The life cycle is basically is set of certain stages which occur at a certain time. MVC actually defined in two life cycles, the application life cycle, and the request life cycle. The Starting point for every MVC application begins with routing.


1 Answers

You should not be doing this task from your web application as Phil Haack nicely explains it in his blog post.

How is this sort of thing normally done?

You could perform this task from a Windows Service or even a console application that is scheduled to run at regular intervals using the Windows Scheduler.

like image 139
Darin Dimitrov Avatar answered Oct 16 '22 14:10

Darin Dimitrov