Here is an excerpt from the Use streaming in ASP.NET Core SignalR article by Microsoft:
private async Task WriteItemsAsync(
ChannelWriter<int> writer,
int count,
int delay,
CancellationToken cancellationToken)
{
try
{
for (var i = 0; i < count; i++)
{
// Check the cancellation token regularly so that the server will stop
// producing items if the client disconnects.
cancellationToken.ThrowIfCancellationRequested();
await writer.WriteAsync(i);
// Use the cancellationToken in other APIs that accept cancellation
// tokens so the cancellation can flow down to them.
await Task.Delay(delay, cancellationToken);
}
}
catch (Exception ex)
{
writer.TryComplete(ex);
}
writer.TryComplete();
}
If there's an exception, it will first call writer.TryComplete(ex), and then writer.TryComplete(). In other words, it calls TryComplete (albeit different overloads) twice.
Is this necessary? Should I add a return statement after writer.TryComplete(ex) to avoid calling it twice? Or does the second writer.TryComplete() serve some meaningful purpose after calling the former?