New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggestion: Exposing Cancellation Tokens #20
Comments
After listening to Steve's episode about the project on .NET Rocks this past weekend, when he compared it to MediatR, this was the first thing that popped into my head. Considering most library providers support some form of To me as a consumer, it seems strange in the first place to not offer @ardalis I'm curious of your thoughts and the viability of this change, and if you have any input, I'm all ears. |
I'd be happy to add an optional CancellationToken on HandleAsync - that makes perfect sense. That should work, right? |
Also @seangwright sorry for not responding to this sooner. Juggling a lot these days. :) |
@ardalis Correct me if I'm wrong, but I don't think optional parameters work in |
I think you're right. I'm fine with the breaking change. We can just rev the version to 2.0. I think it makes sense to expose them, despite the fact I don't often implement them in most of the apps I build (that expect requests to take < 500ms each). If one of you wants to make a PR adding them I'd take it, thanks. |
When can we expect a NuGet package update? If others try to mimic the sample application in master they won't be able to without realizing the latest NuGet package doesn't use cancellation tokens yet. |
I'll try and get an update out this week. |
Done and in 2.0: |
Current State
Currently, if developers want to control operation cancellation through
CancellationToken
, they are a little stuck, since theHandleAsync()
method doesn't have aCancellationToken
parameter.MVC auto injects these values if they are present.
Motivation
In an app that renders HTML for the browser, this might not be as important, but in client side JavaScript applications, it's not uncommon for for a user to interact with multiple parts of the UI in quick succession (like typing in a search box) that would cause previous XHR requests to be canceled since those responses would be discarded by the client anyway - it's only interested in the latest response.
ASP.NET Core will still handle the cancellation and not send the response, but the developer's pipeline of operations continue to execute on the previous request thread while the newer request is handled.
Many external service libraries (DB, HTTP) accept
CancellationToken
s to halt operations.Solution
It would be nice to be able to support cancellation from end-to-end with ApiEndpoints.
I believe we'd need to change the
HandleAsync()
API to accept aCancellationToken token
as the last parameter, which means a breaking change.Thoughts
I'd be willing to PR this if you are interested in the idea.
I'm also wondering, since it would be a breaking change, if there were any other solutions to problems you'd like to roll in with it, like supporting multiple method params (not sure how... maybe multiple generics like the BCL does it with
Func<>
andAction<>
?)The text was updated successfully, but these errors were encountered: