dotnet add package Soenneker.Utils.Cancellation
- Register the interop within DI (
Program.cs
).
public static async Task Main(string[] args)
{
...
builder.Services.AddCancellationUtil();
}
- Inject
ICancellationUtil
where you wish to set theCancellationToken
(typically from an API Controller)
_cancellationUtil.Set(cancellationToken);
- Inject
ICancellationUtil
where you wish to retrieve theCancellationToken
(typically when accessing another API, or database etc):
// 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();