https://github.com/soenneker/soenneker.utils.cancellation
A utility library allowing for easy CancellationToken usage
https://github.com/soenneker/soenneker.utils.cancellation
cancellation cancellationtoken csharp dotnet
Last synced: 17 days ago
JSON representation
A utility library allowing for easy CancellationToken usage
- Host: GitHub
- URL: https://github.com/soenneker/soenneker.utils.cancellation
- Owner: soenneker
- License: mit
- Created: 2023-02-26T01:29:37.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-22T15:09:52.000Z (over 1 year ago)
- Last Synced: 2024-05-22T15:15:44.771Z (over 1 year ago)
- Topics: cancellation, cancellationtoken, csharp, dotnet
- Language: C#
- Homepage: https://soenneker.com
- Size: 321 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.nuget.org/packages/Soenneker.Utils.Cancellation/)
[](https://github.com/soenneker/soenneker.utils.cancellation/actions/workflows/publish-package.yml)
[](https://www.nuget.org/packages/Soenneker.Utils.Cancellation/)#  Soenneker.Utils.Cancellation
### A utility library allowing for easy CancellationToken usage## Installation
```
dotnet add package Soenneker.Utils.Cancellation
```## Usage
1. Register the interop within DI (`Program.cs`).
```csharp
public static async Task Main(string[] args)
{
...
builder.Services.AddCancellationUtil();
}
```2. Inject `ICancellationUtil` where you wish to set the `CancellationToken` (typically from an API Controller)
```csharp
_cancellationUtil.Set(cancellationToken);
```3. Inject `ICancellationUtil` where you wish to retrieve the `CancellationToken` (typically when accessing another API, or database etc):
```csharp
// is not guaranteed to be non-null (if it never was set within scope), but is specified thus for ease of use
var cancellationUtil = _cancellationUtil.Get();
```