My web app has to send some emails every 1,2,7 days based on some user generated data on my .NET 5 MVC web app. The app it's quite complex as it's 3 portals running togheter and interacting with each other.
The triggers are simple, for example: If user didn't complete this envoice in 1 day, send a reminder mail. If user didn't got a feedback (and inserted it in our web app form and saved) send mail reminder every 2 days.
And so on....
Usually I do this with Windows services as you can easily put up a console program in C#, schedule it and forget.
This time, I would like to attempt doing it with something I saw once in a project and never used: IHostedService (or IHost) or BackgroundService in .NET 5.
Both for learning something new, both so that I can reuse most of the database code through dependency injection
I documented myself a bit and this is what I found:
Asp.Net - Scheduled Task using IHostedService
.NET Web Service & BackgroundWorker threads
How to run BackgroundService on a timer in ASP.NET Core 2.1
Async timer in Scheduler Background Service
And About dependency injection:
.net core dependency injection to hosted service So it has something to solve about the whole Singleton thing.
Now, this seems somewhat unreliable or something that at first glance looks like an elegant solution but will give lot of hair pulling later with misteryous behaviours and errors and has a lot of issues.
- What are the use cases for these services? They look like more of a fancy addon for the framework which are rarely used as their noticeable limitations hinders them.
- Should I try to use them or go for the windows service (and so bring into it all the db code I wrote)?