ASP.NET Core exposes `HttpContext.RequestAborted`. Accept it as a `CancellationToken` action parameter and pass the same token through every cancellable layer. Cancellation is cooperative: each operation must observe the token. Do not replace it with `CancellationToken.None`, and do not convert a client disconnect into a generic server error. ```csharp [HttpGet("{id:guid}")] public async Task<ActionResult<OrderDto>> Get( Guid id, CancellationToken cancellationToken) { var order = await repository.FindAsync(id, cancellationToken); if (order is null) return NotFound(); using var request = new HttpRequestMessage( HttpMethod.Get, $"risk/{order.CustomerId}"); using var response = await httpClient.SendAsync(request, cancellationToken); response.EnsureSuccessStatusCode(); return Ok(Map(order)); } ``` For an application timeout, link it with the request token using `CancellationTokenSource.CreateLinkedTokenSource`, then call `CancelAfter`. Catch `OperationCanceledException` only when you can add meaning or distinguish timeout from caller cancellation; otherwise let the framework complete cancellation handling. Never reuse a disposed token source.
Krishna Yadav · · 0 likes