Samir.B
Samir.B

Reputation: 33

How can i execute a worker service once in .net core 3.0?

I'm using a service worker template in .net core 3.0 . What i would like to do is execute this service only once if my parameter "ExecuteOnce" is set to true in the appsettings.json .

Program.cs :

public class Program
    {
        public static IServiceProvider Services { get; set; }
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((hostContext, config) =>
                {
                    if (hostContext.HostingEnvironment.IsProduction())
                        config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
                    else
                        config.AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true);

                    config.SetBasePath(Directory.GetCurrentDirectory());
                })
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<Worker>();
                });
        public static void Main(string[] args)
        {
                CreateHostBuilder(args).Build().Run();
        }
    }

Worker.cs :

public class Worker : BackgroundService
    {
        private readonly IConfiguration _configuration;
        public Worker(ILogger<Worker> logger, IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public override Task StartAsync(CancellationToken cancellationToken)
        {
            return base.StartAsync(cancellationToken); ;
        }

        public override Task StopAsync(CancellationToken cancellationToken)
        {
            return base.StopAsync(cancellationToken);
        }

        protected override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                // Bit of logic here...

                if (_configuration.GetValue<bool>("TaskConfig:ExecuteOnce"))
                    // TODO HERE : stop this service
                else
                    await Task.Delay(_configuration.GetValue<int>("TaskConfig:TaskDelayMs"), new CancellationToken());
            }
        }
    }

I've tried : -awaiting a Task.TaskCompleted -breaking the loop -calling StopAsync()

But each time i've stumbled upon some limitations , what is the correct way to implement this?

Upvotes: 3

Views: 3199

Answers (1)

RB.
RB.

Reputation: 37182

Use the IHostApplicationLifetime - with this you can tell your application to shut itself down.

public class Worker : BackgroundService
{
    private readonly IHostApplicationLifetime _hostLifetime;

    public Worker(IHostApplicationLifetime hostLifetime) 
    {
        _hostLifetime = hostLifetime;    
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (true)
        {
            DoWork();
            if (RunOnlyOnce())
            {
                _hostLifetime.StopApplication();
            }
        }
    }
}

Upvotes: 12

Related Questions