2

I have a BackgroundService that I require to run every 4 hours. I was under the impression it would run only a single instance. Recently I noticed some duplicate data and it turns that my BackgroundService is running multiple instance and each instance seems to be isolated eg can't share state information.

I have it as a HostedService in the start up file:

services.AddHostedService<MyBackgroundTask>();

Example of the background service:

  public class MyBackgroundTask : BackgroundService
{

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {

        await Task.Yield();

        while (!stoppingToken.IsCancellationRequested)
        {

            try
            {
                // do some calculations

                await Task.Delay(TimeSpan.FromHours(4), stoppingToken);
            }
            catch (Exception e)
            {

                continue;
            }

        }
    }
}

I have tried adding it as a singleton, but it doesn't seem to execute on load, any advise on what I should try?

Mokky Miah
  • 1,213
  • 2
  • 11
  • 29
  • 3
    _it turns that my BackgroundService is running multiple instance_ - how you find out that multiple instances of `MyBackgroundTask` is running? – Fabio Aug 03 '21 at 06:39
  • Is your issue that `ExecuteAsync` can be executed multiple times at once or that `MyBackgroundTask` can have multiple instances? – DekuDesu Aug 03 '21 at 06:52
  • Does this answer your question? [What is the correct way to create a single-instance WPF application?](https://stackoverflow.com/questions/19147/) and [Run the current application as Single Instance and show the previous instance](https://stackoverflow.com/questions/50552592/) and [How to ensure only one instance of IHostedService is running within a .NET core 2.2 Web API](https://stackoverflow.com/questions/58116722/) and [What is the simplest way to run a single background task from a controller in .NET Core?](https://stackoverflow.com/questions/62222712/) –  Aug 03 '21 at 07:30
  • 3
    Are you running one instance of your application or is this a load-balanced multiple-instance service? – Martin Ullrich Aug 03 '21 at 07:32
  • [Use scoped services within a BackgroundService](https://learn.microsoft.com/en-us/dotnet/core/extensions/scoped-service) • [Create a Windows Service using BackgroundService](https://learn.microsoft.com/en-us/dotnet/core/extensions/windows-service) –  Aug 03 '21 at 07:35
  • Hello, I confirmed it's running multiple instances by having the code generate a guide and logging. I found two distinct values, hence I concluded it's running two instance. We have our web application running on a in house IIS server. We do have IIS set to auto spawn workers, I think it's running the background service per worker. – Mokky Miah Aug 03 '21 at 17:41
  • `AddHostedService()` will add your service as a Singleton anyway. You said you have multiple worker processes set up in IIS, that means your app is started multiple times, each one having it's own singleton `MyBackgroundTask` running. If you truly want only one to run at a time and you still want the 2 worker processes for your server, I suggest running the background task in a completely separate process, or have it be regulated using another resource (like a DB, file, registry, etc.) – Andor Baranyi Aug 04 '21 at 14:43

0 Answers0