0

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.

  1. 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.
  2. Should I try to use them or go for the windows service (and so bring into it all the db code I wrote)?

1 Answers1

0

Here is some worth considering points that I think should clear out our foggy:

I documented myself a bit and this is what I found

In short, they are all class instance inherit from IHostedService, .Net Core provide us an abstract base class that called BackgroundService, which is obviously, inherit from IHostedService too, as we can see here.

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.

Using BackgroundService for taskd that related to:

  • Schedule taska that should running interval.
  • Long running task.
  • Serve as background process for our business logic.
  • Many and many more as long as it suit our cases...

So it has something to solve about the whole Singleton thing.

If we need some scope or transient service inside it, I strongly recommend you to take sometime investigate on Dependency injection, take the its general idea, that won't waste your time and we usually, the idea was the same across all languages and library.

This simple block code here solve that thing in BackgroundService

public abstract class MyProcessor : Microsoft.Extensions.Hosting.BackgroundService
{
    protected readonly IServiceScopeFactory ServiceScopeFactory;

    protected MyProcessor(IServiceScopeFactory serviceScopeFactory)
    {
         ServiceScopeFactory = serviceScopeFactory;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        using var scope = ServiceScopeFactory.CreateScope();
        var myService = scope.ServiceProvider.GetRequiredService<EmailService>();
        //... Any logic below as you wish
        // Put them in a loop with [await Task.Delay()] to make it run interval
    }
}

Should I try to use them or go for the windows service (and so bring into it all the db code I wrote)?

An absolutely yes, windows service tight you with windows, which won't be so friendly on container area now. And more than that, feel free to have as much BackgroundService as we wished on a single project and it can be run anywhere.

Gordon Khanh Ng.
  • 1,352
  • 5
  • 12