I have a windows service for netcore 2.0 which is based on this example: https://www.stevejgordon.co.uk/running-net-core-generic-host-applications-as-a-windows-service
The main hosted service in my case is subscribed to a message queue and processes the messages sequentially (an event is raised by the queue api each time a new message appears). If any messages cannot be processed they are moved to a different queue to be retried (x number of times, with exponential back-off). And so this needs to be done on a separate thread so as not to cause delays on the processing of the main queue
Is it ok to configure 2 HostedServices:
var builder = new HostBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<MyMsgProcessingService>();
services.AddHostedService<MyRetryService>();
});
This does start and it does hit the StartAsync
method (on the IHostedService
interface) of each service
Or are you only allowed to have (is a good idea to have) one IHostedService
implementation?
If two is OK, will the work on the slow service hold up the wok on the main service?
If only one, is it best to just spin up another thread to do the work for the retry queue? And how would I ensure that the new thread lives for the whole time the service is running? a while
loop seems inappropriate as I'm just waiting for an event to be raised
e.g.
public Task StartAsync(CancellationToken cancellationToken)
{
Task t = Task.Run(() => ProcessRetrys(), cancellationToken);