I have the following action method with async
and await
keywords:
[HttpPost]
public async Task<ActionResult> Save(ContactFormViewModel contactFormVM)
{
if (domain.SaveContactForm(contactFormVM) > 0)// saves data in database
{
bool result = await SendMails(contactFormVM);//need to execute this method asynchronously but it executes synchronously
return Json("success");
}
return Json("failure");
}
public async Task<bool> SendMails(ContactFormViewModel contactFormVM)
{
await Task.Delay(0);//how to use await keyword in this function?
domain.SendContactFormUserMail(contactFormVM);
domain.SendContactFormAdminMail(contactFormVM);
return true;
}
In the above code, once the database operation is finished I want to immediately return Json()
result and then call the SendMails()
method which should execute in the background. What changes should I make to the above code?