Targaryen
Targaryen

Reputation: 1091

MVC - Run scheduled task even if nobody logs into app

I have a hosted ASP.NET MVC5 web app. Is there any way to get the app to run a "scheduled" task even if nobody logs into the app? Or is my only choice to use the App Start when the app first runs?

I need to send an email to my users first thing each morning. Is there a reasonable way to do this with the MVC5 app or am I going to have to set up a Windows service?

Upvotes: 1

Views: 3492

Answers (8)

Gafar Popoola
Gafar Popoola

Reputation: 21

Hangfire to the rescue. This is perhaps the easiest and more reliable way to achieve this task. It comes with a dashboard that makes managing and monitoring your background tasks effortless.

Upvotes: 0

Rakesh Guranani
Rakesh Guranani

Reputation: 552

Please check the below URL, using which you can make sure that your web application is always up, even if no body is logged into your application, or if your application is idle for long time

You just need to configured your server per below and make sure to start your jobs.

http://developers.de/blogs/damir_dobric/archive/2009/10/11/iis-7-5-and-always-running-web-applications.aspx

Upvotes: -1

D. Kermott
D. Kermott

Reputation: 1673

The common way to do this is with the Windows Task Scheduler. The problem with calling START or some other command line parameter is the opened browser may never close or it might close when the task times out.

I wrote a console app to make a call to a website and wait for a response.

Imports System.IO
Imports System.Net

Module Module1

    Sub Main()
        Dim sw As New Stopwatch
        sw.Start()

        Try
            Dim args = Environment.GetCommandLineArgs.ToList
            If args.Count = 1 Then Throw New Exception("WebsiteWaitResponse url [user] [password]")

            Console.WriteLine("{0:c} WebsiteWaitResponse {1:g}", sw.Elapsed, Now())

            Dim web As New WebClient
            If args.Count > 2 Then web.Credentials = New NetworkCredential(args(2), args(3))
            Dim results = web.DownloadString(args(1))
            Console.WriteLine(results)

        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try

        Console.WriteLine("{0:c} WebsiteWaitResponse Complete", sw.Elapsed)
        End
    End Sub

End Module

Create a scheduled task which calls this app with command line parameters as follows:

MyConsoleApp url [userid] [password]

where [userid] and [password] are optional and used for ntlm authentication

MyConsoleApp http://mywebsite/controller/function mydomain\myuser mypassword

Upvotes: 0

user2069723
user2069723

Reputation: 167

I think the question comes down to, do you need the ability to start/stop the service and have the webapp still running?

I personally try to avoid setting up a windows service because it adds another layer of complexity that can break/not work. If you use quartz or just a basic timer in your web app, the scheduling is guaranteed to run when your app runs.

With in-app scheduling you can install your webapp with a simple file copy.

Sure, there are situation when you need to do heavy background jobs, then you might want to consider a separate batch job project with a windows service... but for sending out a couple of email, just use in-app scheduling.

Upvotes: 0

Yair Nevet
Yair Nevet

Reputation: 13013

I would not involve an email sending job with MVC application, since if you think about it, an MVC application concern is to work by the Request-Response model, on which scenario do you see it start a new job?

If you have an access to your users emails, just create a simple Console Application or a Windows Service to do that work and set a scheduling for it using the Windows Task Scheduler or any other task scheduling tool.

In addition, if you're enforced to do it within your MVC application:

  • Read is a nice old post by Jeff Atwood about how to create a job inside ASP.NET application: Easy Background Tasks in ASP.NET
  • Create and schedule a call to an Action in your MVC application that will do that email sending work
  • Use Quartz.NET third-party library for creating scheduled background tasks in Web-Applications

Upvotes: 2

Stan
Stan

Reputation: 26511

You can create a singleton and in your ApplicationStart(); that will launch itself every 24h and then send emails. It will include locking that particular thread for 24h.

It's a very bad approach but it seems that you don't have any other options when you're on shared hosting with no access to actual system.

Upvotes: 1

SlaterCodes
SlaterCodes

Reputation: 1139

Most people recommend a windows service. However, a reasonable way to do this would be using a scheduling framework like Quartz .NET

http://quartznet.sourceforge.net/

I prefer this because then my jobs/schedules travel with my application and when I deploy on a new box I don't have to setup a service or anything, everything is embedded in the MVC5 application. Quartz also has the ability to sync between servers via a db if you have a load-balanced environment (like I do) and it works well enough for me. Also using the DB as the job store makes sure that jobs persist between deployments and application restarts because by default jobs are in memory.

Upvotes: 4

meziantou
meziantou

Reputation: 21377

Don't use a Windows service, instead you should use the Windows Task Scheduler.

Just create a Console Application and register it in the scheduler.

Upvotes: 1

Related Questions