Search code examples
asp.net-corebackgroundcqrsmediatr

Handling long blocking API calls in ASP.NET Core


I'm building an API that will handle http calls that perform much work immediately.

For example, in one controller, there is an action (Post request) that will take data in the body, and perform some processing that should be done immediately, but will last for about 1 to 2 minutes.

I'm using CQRS, with Mediatr, and inside this post request, I call a command to handle the processing.

Taking this into consideration, I want the post request to launch the command, then return an Ok to the user though the command is still running in the background. The user will be notified via email once everything is done.

Does someone know any best practice to accomplish this?

Though I'm using MediatR, sending the request to the handler, the call is not executed in parallel.

[HttpPost]
public async Task<IActionResult> RequestReports([FromBody] ReportsIntent reportsIntent)
{
     await _mediator.Send(new GetEndOfWeekReports.Command(companyId, clientId, reportsIntent));
     return Ok();
}

Solution

  • Does someone know any best practice to accomplish this?

    Yes (as described on my blog).

    You need what I call the "basic distributed architecture". Specifically, you need at least:

    1. A durable queue. "Durable" here means "on disk; not in-memory".
    2. A backend processor.

    So the web api will serialize all the necessary data from the request into a queue message and place that on the queue, and then return to the client.

    Then a backend processor retrieves work from that queue and does the actual work. In your case, this work concludes with sending an email.

    In a way, this is kind of like MediatR but explicitly going out of process with the on-disk queue and a separate process being your handler.