Issue
I have some process that take long processing time that client not need the response immediately. I’ve tried below code without success.
[HttpPost, Route("run")]
public async Task Run()
{
_ = this.LongProcess().ConfigureAwait(false);
return await Task.CompletedTask;
}
The service still take time until my LongProces finish before return to the client.
How can I make the Run method return to the client promptly ?
Solution
How can I make the Run method return to the client promptly?
You need a basic distributed architecture. I recommend:
- A durable storage system, such as Azure Queues or AWS Simple Queue Service.
- An independent processor, such as Azure Functions or AWS Lambdas.
Then, your API enqueues the message and returns:
[HttpPost, Route("run")]
public async Task Run()
{
var message = new Message();
await _queueService.EnqueueAsync(message);
return;
}
and the independent processor dequeues messages and handles them:
async Task HandleMessage(Message message)
{
await LongProcess().ConfigureAwait(false);
}
Answered By – Stephen Cleary
Answer Checked By – Willingham (BugsFixing Volunteer)