madelson/CancellationProvider: .NET cancellation abstraction for flowing CancellationToken via dependency injection
github.com·3h
💜.NET
Preview
Report Post

CancellationProvider

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...

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help