I have the following code in my BackgroundService
:
public async Task ExecuteAsync(CancellationToken cancellationToken)
{
await GetElements(cancellationToken)
.ToObservable()
.Buffer(TimeSpan.FromMinutes(1), 100)
.Select(elements => Observable.FromAsync(() => ProcessElementsAsync(
elements, cancellationToken)))
.Concat()
.ToTask(cancellationToken);
}
private async IAsyncEnumerable<Element> GetElements(
[EnumeratorCancellation] CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested) // <-- Breakpoint is here
{
yield return await _queue.DequeueElementAsync(cancellationToken);
}
}
When I run the app, the breakpoint is never hit. It looks as if the GetElements
is never actually executed.
When I replace ExecuteAsync
with this code, the breakpoint in GetElements
does get hit:
public async Task ExecuteAsync(CancellationToken cancellationToken)
{
GetElements(cancellationToken)
.ToObservable()
.Subscribe(val =>
{
_ = 1;
});
await Task.Delay(Timeout.InfiniteTimeSpan, cancellationToken);
}
What's wrong with the first implementation?