skip to Main Content

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


  1. Chosen as BEST ANSWER

    I achieved this by using .ConfigureAwait(false). For example:

    if (result.IsSuccess)
    {
       SendSMSAsync(smsDetails).ConfigureAwait(false);
       SendEmailAsync(emailDetails).ConfigureAwait(false);
       ThirdPartyAsync(moreDetails).ConfigureAwait(false);
    }
    

    More information can be found here


  2. Instead of await, create a task and call Task.Run. This will run without waiting for the task to return.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search