MVC - Run a scheduled task even if no one logs into the application

I have an ASP.NET MVC5 web application. Is there a way to force the application to run a "scheduled" task even though no one is logging into the application? Or is my only choice to use the Start app the first time I launch the app?

I need to send an email to my users first thing every morning. Is there a sane way to do this with an MVC5 app, or will I have to configure a Windows service?

+1


source to share


8 answers


Most people recommend the Windows service. However, a sane way to do this would be to use 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 in a new window, I don't need to configure the service or whatever, everything is built into the MVC5 application. Quartz also has the ability to sync between servers via db if you have a load balanced environment (like me) and it works well enough for me. In addition, using the database as a job store ensures jobs persist between deployments and application restarts, since jobs are kept in memory by default.

+3


source


I would not go into email sending a job using an MVC application, since if you think about it, the problem with the MVC application is with the Request-Response model, what scenario do you see it starting a new job?

If you have access to users' emails, just create a simple console application or Windows service to do the job and set up scheduling for it using Windows Task Scheduler or any other task scheduling tool.



Also, if you enforce this in your MVC app:

  • Read a good old post by Jeff Atwood on How to Create Work Inside an ASP.NET Application: Simple Background Tasks in ASP.NET
  • Create and assign an Action call in your MVC application that will do this job via email.
  • Use Quartz.NET 3rd Party Library to Create Scheduled Background Tasks in Web Applications
+2


source


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

Just create a console app and register it with the scheduler.

+1


source


You can create a singleton in yours too ApplicationStart();

, which will run every 24 hours and then send emails. It will block this thread for 24 hours.

This is a very bad approach, but it seems like you have no other options when you are on shared hosting without access to the real system.

+1


source


I think this question boils down to this: Do you need the ability to start / stop a service and run in a webapp yet?

I personally try not to install a Windows service because it adds another layer of complexity that might break / not work. If you are using quartz or just a basic timer in your web application, scheduling will be done when your application starts up.

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

Of course, there are situations where you need to do heavy background jobs, then you might need to consider a separate batch job project using Windows Service ... but to send multiple emails just use in-app scheduling.

0


source


The general way to do this is with Windows Task Scheduler. The problem with calling START or some other command line parameter is that the opened browser can never close or it can close when the task is running.

I wrote a console application to call the 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 that calls this application with command line parameters as follows:

MyConsoleApp url [userid] [password]

      

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

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

      

0


source


Hangfire to the rescue. This is perhaps the easiest and most reliable way to solve this problem. It comes with a dashboard that makes it easy to manage and monitor your background tasks.

0


source


Please check the URL below, with which you can ensure that your web application is always running, even if no authority is registered to your application, or if your application has been down for a long time

You just need to set up your server below and don't forget to get started.

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

-1


source







All Articles