Skip to content

Added async overload of ChangeToken.OnChange#129624

Draft
svick wants to merge 4 commits into
dotnet:mainfrom
svick:async-changetoken-onchange
Draft

Added async overload of ChangeToken.OnChange#129624
svick wants to merge 4 commits into
dotnet:mainfrom
svick:async-changetoken-onchange

Conversation

@svick

@svick svick commented Jun 19, 2026

Copy link
Copy Markdown
Member

No description provided.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-primitives
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends Microsoft.Extensions.Primitives.ChangeToken with Task-returning OnChange overloads so consumers can run asynchronous work and delay re-registration until that work completes, and it refactors the internal registration logic to share more between sync and async paths.

Changes:

  • Add ChangeToken.OnChange(Func<IChangeToken?>, Func<Task>) and ChangeToken.OnChange<TState>(Func<IChangeToken?>, Func<TState, Task>, TState) public overloads.
  • Refactor registration implementation into shared base + sync/async derived registrations.
  • Update/add unit tests covering async behavior, disposal while in-flight, and overload binding.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/libraries/Microsoft.Extensions.Primitives/src/ChangeToken.cs Adds async overloads and refactors internal registration to support deferred re-registration after Task completion.
src/libraries/Microsoft.Extensions.Primitives/ref/Microsoft.Extensions.Primitives.cs Updates ref assembly to include the new public overloads.
src/libraries/Microsoft.Extensions.Primitives/tests/ChangeTokenTest.cs Adjusts existing tests for overload binding and adds new tests for async/disposal/coalescing semantics.

Comment on lines +272 to +283
private async Task AwaitConsumerAndRegisterCallback(Task consumerTask, IChangeToken? token)
{
try
{
await consumerTask.ConfigureAwait(false);
}
finally
{
// We always want to ensure the callback is registered
RegisterChangeTokenCallback(token);
}
}
Comment on lines +70 to +74
/// <remarks>
/// Exceptions from <paramref name="changeTokenProducer"/> are propagated to the caller of this method or to the code that triggers the change token.
/// Synchronous exceptions from <paramref name="changeTokenConsumer"/> are propagated to the code that triggers the change token.
/// Asynchronous exceptions from <paramref name="changeTokenConsumer"/> are left unobserved.
/// </remarks>
@tarekgh tarekgh added this to the 11.0.0 milestone Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants