Skip to main content

Abstraction for flowing CancellationToken via dependency injection

Total Downloads
11.9K
11,886
Published
Jun 29, 2024
2y ago

Get Started

dotnet add package CancellationProvider --version 1.0.0
ProductVersions
.NET
net8.0
.NET Standard
netstandard2.0

README

CancellationProvider NuGet Status

CancellationProvider is a tiny .NET library to facilitate the flow of CancellationToken through your code.

More and more .NET APIs are cancelable, which is great for writing robust, hang-free code. However, passing an instance of CancellationToken down through the call stack to reach all of these cancelable method calls is rather clunky. CancellationProvider solves this by using dependency injection to flow the token, thereby allowing services that call cancelable APIs to consume it without other services being aware:

// in Program.cs (ASP.NET Core app)
builder.Services.AddScoped(p => new CancellationProvider(p.GetRequiredService<IHttpContextAccessor>().HttpContext?.RequestAborted));

// OR in Program.cs (Console app)
using CancellationTokenSource source = new();
Console.CancelKeyPress += (o, e) =>
{
    source.Cancel();
    e.Cancel = true; // Prevent the process from terminating immediately
};
services.AddSingleton(new CancellationProvider(source.Token));

// in MyService.cs
class MyService(CancellationProvider cancellationProvider)
{
    public async Task DoSomethingAsync()
    {
        ...
        await command.ExecuteNonQueryAsync(cancellationProvider.Token); // use the token for cancelable operations
        ...
    }
}

Scopes

Sometimes, you might still want more fine-grained control over cancellation, for example setting a timeout for a particular operation or eschewing cancellation during a critical step. CancellationProvider offers a scoping mechanism to customize the provider's behavior in logical regions of code:

class MyService(CancellationProvider cancellationProvider, OtherService otherService)
{
    public async Task DoStuff()
    {
      // run some code with a strict timeout. Assumes OtherService and/or its dependencies are leveraging CancellationProvider
      using CancellationTokenSource timeoutSource = new(TimeSpan.FromSeconds(5));
      using (cancellationProvider.BeginScope(timeoutSource.Token))
      {
        await otherService.DoSomethingAsync();
      }

      // avoid canceling a critical operation. Assumes OtherService and/or its dependencies are leveraging CancellationProvider
      using (cancellationProvider.BeginCleanScope())
      {
        await otherService.DoSomethingAsync();
      }
    }
}

Scopes are also useful when you have singleton services that want to use the cancellation provider in a web application. For example in an ASP.NET Core app you could do this:

// register a singleton CancellationProvider linked to the host lifetime
builder.Services.AddSingleton(p => new CancellationProvider(p.GetRequiredService<IHostApplicationLifetime>().ApplicationStopping));

app.Use(async (context, next) =>
{
    // on each request, enter a scope tied to the request's cancellation token
    using var scope = context.RequestServices.GetRequiredService<CancellationProvider>().BeginScope(context.RequestAborted);
    await next();
});

Release notes

  • 1.0.0
    • Initial release

Downloads by Version

Not enough data for chart

Similar & Alternative Packages

Hand-picked matching NuGet libraries based on tags & ecosystem

Depended On By

Find packages that declare this package as a dependency. NuGet does not provide a reverse-dependency endpoint, so candidates are checked on demand.

Adoption Assessment

Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.

Compatibility metadata lists 2 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.