ASP.NET Core Interview Question #230
How would you handle a long-running operation requested by an HTTP client?
Senior ASP.NET Core Real-World Scenarios Senior Advanced
Quick Interview Answer
Do not keep an HTTP request open for minutes when the work can be performed asynchronously. Validate and accept the request, create a job record, enqueue durable work, and return an identifier that the client can use to query status.
Detailed Explanation
Do not keep an HTTP request open for minutes when the work can be performed asynchronously. Validate and accept the request, create a job record, enqueue durable work, and return an identifier that the client can use to query status.
A 202 Accepted response is commonly suitable when processing has been accepted but is not complete. The worker should update durable job state and handle retries safely.
If work must survive application restarts, do not rely only on an in-memory queue; use durable storage or a suitable external queue/job system.
Code Example
[HttpPost("reports")]
public async Task<IActionResult> StartReport(
CreateReportRequest request,
CancellationToken ct)
{
var jobId = await _jobs.EnqueueAsync(request, ct);
return AcceptedAtAction(
nameof(GetStatus),
new { id = jobId },
new { id = jobId, status = "Queued" });
}