Good day!

I create a web application on ASP.NET MVC SPA. It has its own API, which gives the data in the form of JSON. It is necessary to do so that from time to time, the data were taken from an external source, regardless of whether someone is accessing a web application or not. In other words, you need to collect data on a timer, and send a notification to users, for example, by email.

The question is, how do I implement a web service with a timer? What are the options? How is this done right? Can this be done through WCF, or will it also work when it is accessed?

  • Shad, thanks for the reply! I used to do it with the 2nd option with the help of a wine service, this time they just allocate only the usual hosting for the site, not where to put the service ... The first is not a very practical option. Is there a special add-on on IIS? - devEugene
  • @devEugene, did not meet. - Shad

1 answer 1

You have a need for a background task that would run periodically (regardless of requests to a web application) and perform "useful work": collecting some data, sending notifications, etc. As in most cases, there are many possible solutions. I will offer a couple with whom I met in practice.


1) Background streams directly in a web application. In this case, your application (MVC site or Web service) starts up the necessary background threads that are idle for a specified amount of time and then do their work after it expires. After executing the work, the thread again "falls asleep" for a specified period of time. The advantages of this approach:

  • Relatively easy implementation
  • No need to create separate applications

But there are significant drawbacks:

  • Additional Web application load
  • Increased Web application vulnerability (due to unhandled exceptions in threads, for example)

If you decide to try this approach, be sure to check out this article: The Dangers of Implementing Recurring Background Tasks In ASP.NET . It describes the possible risks and example implementation (English language).


2) A separate console application that performs the necessary task and runs on the scheduler according to a predetermined schedule (a similar option is to create your own Windows service). Pros:

  • Web application download from additional work
  • Ability to run on another machine

Minuses:

  • Requires the creation of a separate application and its configuration