C#C
C#3mo ago
10 replies
Jamie Brown

✅ BackgroundService blocking on Startup

I have a service that spins up a few background services, but when one of them is being started up it is blocking the Host startup and preventing any background services registered later on from starting up.

The gist of what it's doing in the Execute block is this, which is pretty similar to the other services, but whereas where they will return the Task when the host calls StartAsync, this one will continue executing.
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
    _logger.LogInformation("Starting processing");
    while (!stoppingToken.IsCancellationRequested)
    {
        if (_queueService.IsEmpty)
        {
            // Wait until some messages to process come through
            await Task.Delay(_emptyQueueDelay, stoppingToken);
            continue;
        }

        IEnumerable<StreamMessage<MessageType>> messages = _queueService
            .DequeueUpTo(_processingBatchMaxSize);

        await ProcessMessages(messages);
    }
}


Relatively new to working with Background Services, but I can't see where the issue might be 😅
Was this page helpful?