Skip to content
Discussion options

You must be logged in to vote

Hi @MenaceFOP3, I think the reason the TaskCanceledException is thrown by the WithCancellation method and the expected code is not executed is that the enumerator created by the WithCancellation method detects that cancellation was requested well before your code does and throws the exception. As a result, because of the exception, the execution control leaves the await foreach (StreamingChatMessageContent streamingResponse in ChatService) loop and never reaches the if (cancellationToken.IsCancellationRequested) line.

If, for your scenario, you need to detect cancellation via cancellationToken.IsCancellationRequested rather than a cancellation exception, consider this alternative implemen…

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@MenaceFOP3
Comment options

@MenaceFOP3
Comment options

@SergeyMenshykh
Comment options

Answer selected by SergeyMenshykh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants