Open
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 a Action<T>
based callback only (https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.primitives.changetoken.onchange?view=dotnet-plat-ext-6.0). 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
{
public IDisposable OnChange(Func<IChangeToken> changeTokenProducer, Func<Task> changeTokenConsumer);
public 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");
});
Alternative Designs
No response
Risks
None