'How to continue processing after return result from asp.net core controller method?

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 1:[1]

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);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stephen Cleary