Skip to content

Commit 6ffed01

Browse files
Fix streaming wording and example (#19984)
1 parent 0345532 commit 6ffed01

4 files changed

Lines changed: 17 additions & 8 deletions

File tree

aspnetcore/signalr/streaming.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ The following sample shows the basics of streaming data to the client using Chan
5858
> [!NOTE]
5959
> Write to the `ChannelWriter<T>` on a background thread and return the `ChannelReader` as soon as possible. Other hub invocations are blocked until a `ChannelReader` is returned.
6060
>
61-
> Wrap logic in a `try ... catch`. Complete the `Channel` in the `catch` and outside the `catch` to make sure the hub method invocation is completed properly.
61+
> Wrap logic in a [`try ... catch` statement](/dotnet/csharp/language-reference/keywords/try-catch). Complete the `Channel` in a [`finally` block](/dotnet/csharp/language-reference/keywords/try-catch-finally). If you want to flow an error, capture it inside the `catch` block and write it in the `finally` block.
6262
6363
::: moniker range=">= aspnetcore-3.0"
6464

aspnetcore/signalr/streaming/samples/2.1/Hubs/StreamHub.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ private async Task WriteItemsAsync(
2727
int count,
2828
int delay)
2929
{
30+
Exception localException = null;
3031
try
3132
{
3233
for (var i = 0; i < count; i++)
@@ -37,10 +38,13 @@ private async Task WriteItemsAsync(
3738
}
3839
catch (Exception ex)
3940
{
40-
writer.TryComplete(ex);
41+
localException = ex;
42+
}
43+
finally
44+
{
45+
writer.Complete(localException);
4146
}
4247

43-
writer.TryComplete();
4448
}
4549
}
4650
#endregion

aspnetcore/signalr/streaming/samples/2.2/Hubs/StreamHub.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ private async Task WriteItemsAsync(
3232
int delay,
3333
CancellationToken cancellationToken)
3434
{
35+
Exception localException = null;
3536
try
3637
{
3738
for (var i = 0; i < count; i++)
@@ -48,10 +49,12 @@ private async Task WriteItemsAsync(
4849
}
4950
catch (Exception ex)
5051
{
51-
writer.TryComplete(ex);
52+
localException = ex;
53+
}
54+
finally
55+
{
56+
writer.Complete(localException);
5257
}
53-
54-
writer.TryComplete();
5558
}
5659
}
5760
#endregion

aspnetcore/signalr/streaming/samples/3.0/Hubs/StreamHub.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ private async Task WriteItemsAsync(
4848
{
4949
localException = ex;
5050
}
51-
52-
writer.Complete(localException);
51+
finally
52+
{
53+
writer.Complete(localException);
54+
}
5355
}
5456
#endregion
5557

0 commit comments

Comments
 (0)