MVC scheduled mail sending

331 Views Asked by At

I have got an ASP.NET MVC 4 application, and I want it to send a report e-mail every week. I've read about Quartz.NET, but it's too powerful for this easy task. Now I'm trying to use NCron, but it requires an initialiser in the Main() method (with obligatory parameter args):

class Program
{
    static void Main(string[] args)
    {
        Bootstrap.Init(args, ServiceSetup);
    }
}

Is there the way to do this in the Application_Start()? What should I pass as a args param? What other solutions can solve this task?

2

There are 2 best solutions below

2
Jørn Schou-Rode On BEST ANSWER

Author of NCron speaking…

First: I have never myself integrated NCron into a web application, and I am not sure how well it will work. For instance, as Kenneth points out, IIS will shut down your app if it does not receive any traffic, and there might be other hiccups as well.

In order to integrate NCron into a web app, I suggest that you ignore Bootstrap.Init() (designed specifically as an entry point to console apps) and rather work directly with SchedulingService:

using (var service = new SchedulingService())
{
    service.Hourly().Run<DataUpdateJob>();
    service.Daily().Run<RecycleCacheJob>();

    service.Start();
}

Again: I have never done this myself, but please do give it a try, and let me and everyone else know how you fare.

0
Kenneth On

You'll have to look up what ncrone does with those parameters. What this does is pass the command-line arguments of your windows app to the component. If you're using it on a web app, you don't have command-line args so if it needs arguments, you will have to construct the arguments yourself (either hard-coded or from a config-file or a database or ...)

It's also possible that these are optional, then you can just pass in an empty array (but again, check the docs of ncrone)

Also, keep in mind that when your application shuts down (standard that is after 20 minutes without any activity), your cron runner will not wake it up. If that will be the case you either need to keep the application alive by assuring that at least one request is done every 20 minutes or configure IIS to keep it alive always.