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?
