ASP.NET Core MVC Interview Question #187

How would you handle long-running work started from an MVC request?

Senior Practical MVC Scenarios & Production Architecture Senior Advanced

Quick Interview Answer

Do not keep an HTTP request open for lengthy background processing when the work can be queued. Persist or enqueue a job, return an acknowledgement or redirect, and process the job with a hosted/background worker or external job system.

Detailed Explanation

Do not keep an HTTP request open for lengthy background processing when the work can be queued. Persist or enqueue a job, return an acknowledgement or redirect, and process the job with a hosted/background worker or external job system.

The background process should create its own dependency-injection scope, support retries or idempotency where appropriate, and persist job status if the user needs progress information.

Code Example

[HttpPost]
public async Task<IActionResult> GenerateReport(
    ReportRequest model)
{
    var jobId = await _jobs.EnqueueAsync(model);

    return RedirectToAction(
        "Status", new { id = jobId });
}