ASP.NET Core | Change Token

change token in ASP.NET Core is a general-purpose, low-level building block used to track state changes. Let me break it down for you:

  1. IChangeToken Interface:

  2. ChangeToken Class:

    • The ChangeToken class is static and used to propagate notifications that a change has occurred.
    • It also resides in the Microsoft.Extensions.Primitives namespace and is implicitly provided to ASP.NET Core apps.
    • The ChangeToken.OnChange(Func<IChangeToken>, Action) method registers an action to call whenever the token changes:
      • Func<IChangeToken> produces the token.
      • The Action is called when the token changes.
      • The overload ChangeToken.OnChange<TState>(Func<IChangeToken>, Action<TState>, TState) takes an additional TState parameter passed into the token consumer action.
      • The OnChange method returns an IDisposable, and calling Dispose stops the token from listening for further changes and releases its resources.
  3. Example Uses of Change Tokens:

    • Change tokens are used in several areas of ASP.NET Core:
      • File Monitoring: For monitoring changes to files, the IFileProvider’s Watch method creates an IChangeToken for specified files or folders.
      • Cache Evictions: IChangeTokens can be added to cache entries to trigger cache evictions when changes occur.
      • TOptions Changes: The default OptionsMonitor<TOptions> implementation of IOptionsMonitor<TOptions> accepts one or more IOptionsChangeTokenSource<TOptions> instances. Each instance returns an IChangeToken for tracking options changes1.

In summary, change tokens provide a flexible mechanism for tracking changes in various scenarios within ASP.NET Core applications. They allow efficient handling of state changes without unnecessary polling or resource consumption 

Comments

Popular posts from this blog

How will you improve performance of ASP.NET Core Application?

ASP.NET Core | Open Web Interface for .NET (OWIN)