3

This is about ASP.NET MVC background. When I need to call action methods on timely basis, I use such 3rd part tools as HangFire, which runs under the application pool. I want to create a sync job, which reads a .csv file generated by our ERP system and update our custom DB.
But I know that having this sync job as an action method inside of my ASP.NET MVC is not the correct way of doing things, as background jobs should not be part of my web application.

So to handle my sync job I did the following :

  1. Using Visual studio 2012 , I created a new console application and inside its main method I wrote the sync job as follow:-

    class Program
    {
        static void Main(string[] args)
        {
            using (Entities sd = new Entities())
            {
                //code goes here
                sd.SaveChanges();
            }
        }
    }
    
  2. How can I call this console application on timely basis? I am thinking on creating a new task using windows task scheduler, which would be executed each hour and call the application, mainly calls the .application file?
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179

2 Answers2

1

now my question is how i can call this console application on timely basis? . i am thinking on creating a new task using windows task scheduler , which will be executed each hour and call the application, mainly calls the .application file?

Running a program is one of the things the Windows Task Scheduler (TS) lives for. So yes, please do use the TS.

Additional benefits of using the TS are:

  • Able to specify the user account to run the process under
  • Multitude of ways to specify a trigger for when the task should run
  • Conditional mechanisms to control if the task should run (computer idle; power savings)
  • And a jolly nice history of the task when run

The alternative is creating you own scheduling service just to run your app but such a choice is frowned upon due to:

  1. reinventing of the wheel
  2. Windows gets flooded with an inordinate number of proprietary task scheduling services all essentially doing the same thing.

Background Jobs in Web Apps

Though one could arguably spawn/schedule a background thread/worker item in say ASP.NET to invoke some c# code, the danger here is that IIS is unaware that you have done so. It may decide to suspend or recycle your App Pool due to web inactivity and then your scheduled jobs would be sent to the void. Hence why it is not recommended.

Now you could always tell IIS not to timeout your App Pool so that it is not recycled periodically but I suspect that will lead to other issues.

Best not to do so in the first place.

However, there is nothing wrong in ASP.NET using the Task Scheduler API to schedule an operation to run say a console app.

  • so seems that TS is the way to go ,, but not sure if I can create a simple web application let say an asp.net mvc , which allow users to change the TS task scheduler ? is this possible ? –  Jan 13 '16 at 01:35
  • 1
    @JohnJohn Yes. There is a [Task Scheduler COM API](https://msdn.microsoft.com/en-us/library/windows/desktop/aa383606(v=vs.85).aspx) –  Jan 13 '16 at 01:37
  • thanks a lot for your very helpful replies. so generally speaking I can create my console application , then I can create a web application and from the web application users can login to the web application and change the TS settings which runs the console application ? –  Jan 13 '16 at 01:39
  • 1
    @JohnJohn You're welcome. That's exactly right. :) I have revised my answer above to include these titbits –  Jan 13 '16 at 01:41
  • ok got your point , so I can create a configuration screen inside my web application which calls the TS API and schedule the task, but doing so will not force the console application to run under IIS? I mean I will use the web application to schedule the task, not to run it ?is this correct ? –  Jan 13 '16 at 01:47
  • 1
    @JohnJohn That's right. Your IIS code merely tells TS what to run and when and you can also specify under which user account which is kinda neat. TS is independent of IIS. –  Jan 13 '16 at 02:42
  • @JohnJohn You are quite welcome good sir. Thank-you for the accept. Wishing you and your project well! :) –  Jan 13 '16 at 03:25
1

Windows Task scheduler is a perfectly valid way to call a .net console application. I have several that I wrote and support that have been running for years with no issues.

E.J. Brennan
  • 45,870
  • 7
  • 88
  • 116
  • ok thanks for your reply,, so are you also against having such a sync job running as an action method inside my asp.net mvc web application ? and it is better to have thi sync job as a console application ? –  Jan 13 '16 at 01:28