-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelService.cs
28 lines (24 loc) · 893 Bytes
/
ModelService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System.Threading.Channels;
namespace AspNetCoreChannels;
public class ModelService : BackgroundService
{
private readonly ChannelReader<Model> _channelReader;
private readonly ILogger<ModelService> _logger;
public ModelService(ChannelReader<Model> channelReader, ILogger<ModelService> logger)
{
_channelReader = channelReader;
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
// https://devblogs.microsoft.com/dotnet/an-introduction-to-system-threading-channels/
while (await _channelReader.WaitToReadAsync(stoppingToken))
{
while (_channelReader.TryRead(out var item))
{
_logger.LogInformation("Message is `{Message}`", item.Message);
await Task.Delay(5000, stoppingToken);
}
}
}
}