Update and clarify IChangeToken docs - #55773
Open
svick wants to merge 1 commit into
Open
Conversation
svick
requested review from
mrek-msft and
rosebyte
and
a lite review from Copilot
August 28, 2026 14:12
Contributor
There was a problem hiding this comment.
Pull request overview
Updates the conceptual documentation for Microsoft.Extensions.Primitives change tokens to reflect newer IChangeToken/ChangeToken.OnChange behavior and clarify callback semantics.
Changes:
- Refreshes article metadata (
ms.date) and addsai-usagefrontmatter. - Clarifies
IChangeToken.ActiveChangeCallbacksbehavior, including implications for detecting changes. - Expands
ChangeToken.OnChangeguidance to cover nullable producers and .NET 11 async consumer overload behavior (with a link to the related breaking-change article).
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
| :::code source="./snippets/primitives/change/Example.Static.cs" id="Static"::: | ||
|
|
||
| Much like previous examples, you'll need an implementation of `IChangeToken` that is produced by the `changeTokenProducer`. The producer is defined as a `Func<IChangeToken>` and it's expected that this will return a new token every invocation. The `consumer` is an `Action` or `Func<Task>` when not using `state`, or an `Action<TState>` or `Func<TState, Task>` where the generic type `TState` flows through the change notification. | ||
| Much like previous examples, you'll need an implementation of `IChangeToken` that the `changeTokenProducer` produces. The producer is a `Func<IChangeToken?>`. After a change, it should return a new token for the next registration. If it returns `null`, `ChangeToken.OnChange` doesn't register a callback. Dispose the `IDisposable` returned by `OnChange` to unregister the consumer. |
|
|
||
| For a synchronous consumer, pass an `Action` or `Action<TState>`. Exceptions from the producer or consumer propagate to the caller that registers or triggers the token. | ||
|
|
||
| Starting in .NET 11, you can pass a `Func<Task>` or `Func<TState, Task>` for an asynchronous consumer. `ChangeToken.OnChange` waits for the returned task to complete before the consumer can be invoked again. This behavior prevents concurrent consumer calls, but it can combine multiple changes that occur while the task runs into one later callback. An exception thrown before the consumer returns its task propagates to the code that registers or triggers the token. Exceptions that occur after the task is returned are left unobserved by `ChangeToken.OnChange`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Updated conceptual docs for
IChangeTokenwith .Net 11 changes and clarifiedActiveChangeCallbacksbehavior (this was updated in XML docs also in .Net 11).Internal previews
Build report