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 badge1 Answer
Reset to default 1Adding 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.)When the TCP connection is terminated (the server receives a
FIN
orRST
package).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 badge1 Answer
Reset to default 1Adding 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.)When the TCP connection is terminated (the server receives a
FIN
orRST
package).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.
本文标签:
版权声明:本文标题:Understanding Cancellation Tokens in a Web Application (React, TypeScript, .NET, C#) - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1735988681a1375398.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论