Repository navigation
Closed
Labels
api-approvedAPI was approved in API review, it can be implementedarea-Extensions-Primitivesbreaking-changeIssue or PR that represents a breaking API or functional change over a previous release.in-prThere is an active PR which will close this issue when it is mergedneeds-breaking-change-doc-createdBreaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet
Milestone
Description
Background and motivation
Today it's not possible to use ChangeToken.OnChange to execute asynchronous logic before re-subscribing for new updates. The current API has an Action<T> based callback only. We need an async overload to run async logic so people don't end up using async void or blocking when using this helper.
API Proposal
namespace Microsoft.Extensions.Primitives;
public static class ChangeToken
{
// existing
// public static IDisposable OnChange(Func<IChangeToken?> changeTokenProducer, Action changeTokenConsumer);
// public static IDisposable OnChange<TState>(
// Func<IChangeToken?> changeTokenProducer, Action<TState> changeTokenConsumer, TState state);
public static IDisposable OnChange(Func<IChangeToken?> changeTokenProducer, Func<Task> changeTokenConsumer);
public static IDisposable OnChange<TState>(
Func<IChangeToken?> changeTokenProducer, Func<TState, Task> changeTokenConsumer, TState state);
}API Usage
using Microsoft.Extensions.Primitives;
var config = new ConfigurationBuilder().AddJsonFile("config.json", optional: false, reloadOnChange: true)
.Build();
_ = ChangeToken.OnChange(config.GetReloadToken, async () =>
{
await Task.Delay(1000);
Console.WriteLine("Change happened");
});Alternatives (added by @svick)
- Add a
CancellationTokenparameter that's canceled when a following change is detected. This would probably be confusing, since not every caller would want to honor it and can be implemented by users if needed. - Change the name to
OnAsyncChange(or similar). For those that useasynclambda withOnChangetoday, this would be less breaking, but they wouldn't automatically get the benefit of the new version.
Risk (added by @svick)
This change means that existing code that uses an async lambda when calling OnChange will change meaning: previously, it compiled to an async void method; with this change, it will be an async Task method, where the callback is re-registered only once the returned Task completes. This could break some users, though the new behavior should be more correct.
Reacted by lonix1, cocowalla, Daniel Dickerson, cdalmasso, Darrell, James Turner, Stéphane Mitermite, Stefan, Oisin Grehan, masterged and 17 more
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedarea-Extensions-Primitivesbreaking-changeIssue or PR that represents a breaking API or functional change over a previous release.in-prThere is an active PR which will close this issue when it is mergedneeds-breaking-change-doc-createdBreaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet
Activity
maryamariyan commentedon May 10, 2022
Marked for future. @davidfowl is this a critical feature for 7.0?
davidfowl commentedon May 11, 2022
Critical, no but I can take it to API review and do the implementation if needed for .NET 7. It's currently a pit of failure (I filed this because I saw someone use it in a way that would fail).
lonix1 commentedon Jul 7, 2022
This would be highly appreciated!
Another use case. There's double detection of changes to
appsettings.json. There are still open issues for that. Those issues and the docs recommend debouncing the detections. But using the change token approach needs an async overload. (I documented the problem here).dazinator commentedon Nov 9, 2022
I worked around this a while ago: https://github.com/dazinator/Changify/blob/develop/src/Changify/Extensions/ChangeTokenExtensions.cs
Basically, in order to use
ChangeToken.OnChangeyou need:Func<IChangeToken>produces the token.So I wrote the above extension method for
Func<IChangeToken>so you can do this:davidfowl commentedon Nov 9, 2022
One other idea is an IAsyncEnumerable approach?
dazinator commentedon Apr 28, 2023
Here is the implementation I landed on: https://github.com/dazinator/Changify/blob/develop/src/Changify/Extensions/ChangeTokenExtensions.cs#L78
The reason the solution looks like this, to me, is basically because I was already familiar with this
WaitOneAsyncapi - and did not want the cognitive load of trying to make an async equivalent to this:runtime/src/libraries/Microsoft.Extensions.Primitives/src/ChangeToken.cs
Line 55 in 3aaebeb
and a test: https://github.com/dazinator/Changify/blob/develop/src/Changify.Tests/ChangeTokenExtensionsTests.cs#L76
Saying that, even though it seems to be functional, I don't like the fact it's creating two TCS's per async callback.
All in all I think this will serve my purposes for now, until either:
IAsyncEnumerablebased, I could imagine enumerating through an IAsyncEnumerable in a long running background thread for example to process something asynchronously in each iteration in response to a change token firing - basically this would not look like the current ChangeToken.OnChange api.@davidfowl
one thing which I was curious about is that.. the consumer could
unregisterfrom callbacks at any point on another thread for example. Meanwhile an async callback could about to be invoked, or could even be running. It felt to me like it might be nice to supply aCancellationTokeninto the callback which could be signalled if the consumer unsubscribes. However this isn't part of the api at present. Use case would be, the async task does something resource intensive, and if the consumer is no longer interested in being notified, perhaps thats another way of them indicating that even if a callback is currently running, they wish it to be terminated? I am not sure about this perhaps this a seperate thing that they can workaround via their own mechanisms - passing state and such.17 remaining items
bartonjs commentedon Apr 14, 2026
Video
dotnet-policy-service commentedon Apr 14, 2026
Added
needs-breaking-change-doc-createdlabel because this issue has thebreaking-changelabel.needs-breaking-change-doc-createdlabel.Tagging @dotnet/compat for awareness of the breaking change.
Added async overload of ChangeToken.OnChange (#129624)
Added async overload of ChangeToken.OnChange (#129624)
Use async ChangeToken.OnChange overload in FileConfigurationProvider (#…