Let’s assume I have add order API & I need to call 3 APIs if the order is saved successfully. I want to return the response as soon as the order processing is finished without waiting for the 3 APIs to finish.
For example:
var result = ProcessOrder(parameters);
if (result.IsSuccess)
{
await SendSMSAsync(smsDetails); //All 3 APIs includes calling the API, saving status to database & log
await SendEmailAsync(emailDetails);
await ThirdPartyAsync(moreDetails);
}
return result; //I want to return this without waiting for the above 3 APIs to finish
Also, I would like to call again any failed API automatically. What’s the best approach to achieve this?
2
Answers
I achieved this by using
.ConfigureAwait(false)
. For example:More information can be found here
Instead of await, create a task and call Task.Run. This will run without waiting for the task to return.