admin管理员组

文章数量:1026989

I’ve been reading a lot about cancellation tokens, and done some actions to use them, but I still feel like I’m missing the bigger picture.

Context:

Client: React, TypeScript Server: .NET, C# I added a CancellationToken to the signature of the API I expose from the server to the client like this:

[HttpPost("my-api")] public async Task MyApiMethod(string inputVariable1, CancellationToken cancellationToken)

When I do this, the API works the same as it did without the CancellationToken. I propagate the token until I call httpClient.SendAsync and pass the token there as well. Theoretically, this should handle cancellation. But I’m not entirely sure if I’m doing it right.

Questions:

1- Who creates and manages the CancellationToken I added to the API signature? How is it instantiated and with what parameters?

2- How does the server detect that a request has been cancelled from the client side? I read that the server does something when the connection between the client and the server closes. What exactly happens and how?

3- Is this mechanism only for handling cancellations when a browser is closed? What if I want to actively cancel a request that was sent twice with the same key (for example, if the user presses twice on the same button, I want the 2nd click to abort the first work that has started.

Thanks very much!

the whole picture is not clear to me.

I’ve been reading a lot about cancellation tokens, and done some actions to use them, but I still feel like I’m missing the bigger picture.

Context:

Client: React, TypeScript Server: .NET, C# I added a CancellationToken to the signature of the API I expose from the server to the client like this:

[HttpPost("my-api")] public async Task MyApiMethod(string inputVariable1, CancellationToken cancellationToken)

When I do this, the API works the same as it did without the CancellationToken. I propagate the token until I call httpClient.SendAsync and pass the token there as well. Theoretically, this should handle cancellation. But I’m not entirely sure if I’m doing it right.

Questions:

1- Who creates and manages the CancellationToken I added to the API signature? How is it instantiated and with what parameters?

2- How does the server detect that a request has been cancelled from the client side? I read that the server does something when the connection between the client and the server closes. What exactly happens and how?

3- Is this mechanism only for handling cancellations when a browser is closed? What if I want to actively cancel a request that was sent twice with the same key (for example, if the user presses twice on the same button, I want the 2nd click to abort the first work that has started.

Thanks very much!

the whole picture is not clear to me.

Share Improve this question asked Dec 18, 2024 at 14:24 YaelYael 211 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1
  1. Adding a CancellationToken to a controller method is optional, but if you do, ASP.NET will handle it for you and automatically cancel it if the request is canceled. (Consider this answer to decide when to use it or not.)

  2. When the TCP connection is terminated (the server receives a FIN or RST package).

  3. Both. You can actively cancel a request using AbortController. However, in this specific case, your behavior is not always ideal. Either avoid a double-click in the first place (e.g. disable the button until the operation is finished), or make the operation idempotent (or both). You could still cancel the previous operation as an optimization, but keep in mind that it might have already been completed on the server.

I’ve been reading a lot about cancellation tokens, and done some actions to use them, but I still feel like I’m missing the bigger picture.

Context:

Client: React, TypeScript Server: .NET, C# I added a CancellationToken to the signature of the API I expose from the server to the client like this:

[HttpPost("my-api")] public async Task MyApiMethod(string inputVariable1, CancellationToken cancellationToken)

When I do this, the API works the same as it did without the CancellationToken. I propagate the token until I call httpClient.SendAsync and pass the token there as well. Theoretically, this should handle cancellation. But I’m not entirely sure if I’m doing it right.

Questions:

1- Who creates and manages the CancellationToken I added to the API signature? How is it instantiated and with what parameters?

2- How does the server detect that a request has been cancelled from the client side? I read that the server does something when the connection between the client and the server closes. What exactly happens and how?

3- Is this mechanism only for handling cancellations when a browser is closed? What if I want to actively cancel a request that was sent twice with the same key (for example, if the user presses twice on the same button, I want the 2nd click to abort the first work that has started.

Thanks very much!

the whole picture is not clear to me.

I’ve been reading a lot about cancellation tokens, and done some actions to use them, but I still feel like I’m missing the bigger picture.

Context:

Client: React, TypeScript Server: .NET, C# I added a CancellationToken to the signature of the API I expose from the server to the client like this:

[HttpPost("my-api")] public async Task MyApiMethod(string inputVariable1, CancellationToken cancellationToken)

When I do this, the API works the same as it did without the CancellationToken. I propagate the token until I call httpClient.SendAsync and pass the token there as well. Theoretically, this should handle cancellation. But I’m not entirely sure if I’m doing it right.

Questions:

1- Who creates and manages the CancellationToken I added to the API signature? How is it instantiated and with what parameters?

2- How does the server detect that a request has been cancelled from the client side? I read that the server does something when the connection between the client and the server closes. What exactly happens and how?

3- Is this mechanism only for handling cancellations when a browser is closed? What if I want to actively cancel a request that was sent twice with the same key (for example, if the user presses twice on the same button, I want the 2nd click to abort the first work that has started.

Thanks very much!

the whole picture is not clear to me.

Share Improve this question asked Dec 18, 2024 at 14:24 YaelYael 211 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1
  1. Adding a CancellationToken to a controller method is optional, but if you do, ASP.NET will handle it for you and automatically cancel it if the request is canceled. (Consider this answer to decide when to use it or not.)

  2. When the TCP connection is terminated (the server receives a FIN or RST package).

  3. Both. You can actively cancel a request using AbortController. However, in this specific case, your behavior is not always ideal. Either avoid a double-click in the first place (e.g. disable the button until the operation is finished), or make the operation idempotent (or both). You could still cancel the previous operation as an optimization, but keep in mind that it might have already been completed on the server.

本文标签: