[SOLVED] How to continue processing after return result from asp.net core controller method?

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:

  1. A durable storage system, such as Azure Queues or AWS Simple Queue Service.
  2. 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)

Leave a Reply

Your email address will not be published. Required fields are marked *