What’s the recommended way to get a CancellationToken inside a regular HTTP service method?
Am I allowed to simply add it as a method parameter?
I’ve found a few options in the source but I’m not sure which is the intended approach for HTTP requests:
Request.GetCancellationToken()(from JobUtils.cs) - but SetCancellationToken is only called from BackgroundJobs, so this returns CancellationToken.None for regular HTTP requests.- CancellableRequestsFeature - requires the client to send an X-Tag header and explicitly call a cancel endpoint. This seems designed for a specific use case rather than general-purpose cancellation.
- Casting to
NetCoreRequestto access HttpContext.RequestAborted:
This works but feels like it’s reaching outside ServiceStack’s abstraction.public async Task Any(MyRequest request) { var token = ((NetCoreRequest)Request).HttpContext.RequestAborted; await SomeDatabaseQuery(token); }
Is there a built-in way I’m missing to access a cancellation token that’s tied to the HTTP connection lifetime? Would it make sense for ServiceStack to surface HttpContext.RequestAborted through IRequest (or populate Request.Items[“CancellationToken”] for HTTP requests the same way Background Jobs does)?
Thanks!