Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SlowRequestController : Controller
- {
- private readonly IRepository _repository;
- public SlowRequestController(IRepository repository)
- {
- _repository = repository;
- }
- [HttpGet("/slowtest")]
- public async Task<IActionResult> Get(CancellationToken cancellationToken)
- {
- await _repository.LongRunningTask(cancellationToken);
- return Ok();
- }
- }
- public class Repository : IRepository
- {
- public async Task LongRunningTask(CancellationToken cancellationToken)
- {
- // token.ThrowIfCancellationRequested(); or below
- if (token.IsCancellationRequested) // true if RequestAborted by any reason
- {
- Console.WriteLine("Task cancelled");
- return;
- }
- // slow cancellable work
- await Task.Delay(10_000, cancellationToken);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement