ReadAsStringAsync and ReadAsByteArrayAsync without Cancellationtoken

zequion 446 Reputation points
2024-07-25T00:28:16.9133333+00:00

I use the latest version of VS and C# in a .Net 8 project.

I have a function that retrieves the content of a remote file using HttpContent.ReadAsByteArrayAsync() and HttpContent.ReadAsStringAsync();

I can't use the CancellationToken parameter because it doesn't seem to exist.

However, for the HttpClient.GetAsync() command it does exist.

This is discussed at https://stackoverflow.com/questions/17492961/async-read-methods-in-system-net-http-httpcontent-missing-cancellationtoken-over

If the Task is canceled, does the remote action continue?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,009 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hongrui Yu-MSFT 2,465 Reputation points Microsoft Vendor
    2024-07-25T06:20:28.1733333+00:00

    Hi,@zequion. Welcome to Microsoft Q&A. 

    When ReadAsStringAsync and ReadAsByteArrayAsync tasks are cancelled, the behavior of the remote operation depends on the specific implementation and context. Typically, canceling a task causes the operation to stop as quickly as possible, but this does not always mean that the remote operation terminates immediately.

     

    In some cases, canceling a task may cause the local operation to stop, but the remote server may have already started processing the request and continue to execute. To ensure that the remote operation can also be cancelled, it is usually necessary to include a cancellation token (such as a CancellationToken) in the request, and the remote server needs to support cancellation.

     

    In addition, HttpContent.ReadAsByteArrayAsync() and HttpContent.ReadAsStringAsync() can pass a Cancellationtoken parameter. Here are the official documents for the two methods:

     

    https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpcontent.readasbytearrayasync?view=net-8.0#system-net-http-httpcontent-readasbytearrayasync(system-threading-cancellationtoken)

     

    https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpcontent.readasstringasync?view=net-8.0#system-net-http-httpcontent-readasstringasync(system-threading-cancellationtoken)


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 66,621 Reputation points
    2024-07-25T15:46:06.8766667+00:00

    when a HttpClient honors a cancellation token, it closes the connection, and exits the routine. the http protocol does not have much support for cancelling a request. the server may detect a closed connection if it is direct connection (no load balancers, firewalls etc) and it has code to detect a fin message while processing request. typically the server's detection of the closed connection is when it tries to send a response.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.