Skip to main content
.NETFramework4.6.2net8.0net9.0

Asynchronous feature store.

Published
Aug 14, 2024
2y ago
Total Downloads
116.8K
116,842

Install

dotnet add package Microsoft.Extensions.AsyncState --version 9.0.0-preview.7.24412.10
ProductVersions
.NET
net8.0net9.0
Other
.NETFramework4.6.2

README

Microsoft.Extensions.AsyncState

This provides the ability to store and retrieve objects that flow with the current asynchronous context. It has a few advantages over using the AsyncLocal<T> class directly:

  • By abstracting the way the ambient data is stored we can use more optimized implementations, for instance when using ASP.NET Core, without exposing these components.
  • Improves the performance by minimizing the number of AsyncLocal<T> instances required when multiple objects are shared.
  • Provides a way to manage the lifetime of the ambient data objects.

Please note, the implementations of IAsyncState and IAsyncContext<T> are not thread-safe.

Install the package

From the command-line:

dotnet add package Microsoft.Extensions.AsyncState

Or directly in the C# project file:

<ItemGroup>
  <PackageReference Include="Microsoft.Extensions.AsyncState" Version="[CURRENTVERSION]" />
</ItemGroup>

Usage Example

Registering Services

The services can be registered using the following method:

public static IServiceCollection AddAsyncState(this IServiceCollection services)

Consuming Services

The IAsyncContext<T> can be injected wherever async state is needed. For example:

public class MyClass
{
  public MyClass(IAsyncContext<MyState> asyncContext) { Context = asyncContext }

  private IAsyncContext<MyState> Context { get; }

  public async Task DoWork()
  {
    var state = Context.Get();
    // or
    Context.Set(new MyState());
    // or
    if (Context.TryGet(out var state)) { ... }
  }
}

Feedback & Contributing

We welcome feedback and contributions in our GitHub repo.

Changes from Previous Version

No structural changes between v9.0.0-preview.6.24353.1 and v9.0.0-preview.7.24412.10.

Adoption Assessment

A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.

This version is marked prerelease. Validate its API and upgrade path before production use.Compatibility metadata lists 3 target frameworks; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.