Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc 5 asynchronous action method

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?

like image 971
seadrag0n Avatar asked Apr 01 '15 04:04

seadrag0n


People also ask

How do you call async method in MVC action?

In the File menu, click New Project. In the "New Project" dialog box, under Project types, expand Visual C#, and then click "Web". In the Name box, type "Async", then click on Ok. Now, in the dialog box, click on "MVC" under the ASP.NET 4.5.

When would you use asynchronous actions?

Asynchronous actions are best when your method is I/O, network-bound, or long-running and parallelizable. Another benefit of an asynchronous action is that it can be more easily canceled by the user than a synchronous request.

Should I use async controller actions?

When to use Asynchronous Controller. Asynchronous action methods are useful when an action must perform several independent long running operations. Suppose we have three operations which takes 500, 600 and 700 milliseconds. With the synchronous call, total response time would be slightly more than 1800 milliseconds.

Can an action be async?

As a result, actions that complete quickly usually run synchronously. For example, action=getstatus is a synchronous action. Actions that can take a significant time to complete are usually asynchronous.


1 Answers

The await operator is applied to a task in an asynchronous method to suspend the execution of the method until the awaited task completes. The task represents ongoing work.

It sounds like you don't want to wait for the result of SendMails. Think of async and await as tools to consume asynchronous APIs. Specifically, it's really useful to be able to "await" for the result of an "async" task. However, if you don't care about the result of your "async" (e.g. SendMails) task, then you don't need to "await" for the result (i.e. boolean).

Instead, you can simply use Task.Run to invoke your asynchronous task.

[HttpPost]
public async Task<ActionResult> Save(ContactFormViewModel contactFormVM) {
  if (domain.SaveContactForm(contactFormVM) > 0) {// saves data in database 
    Task.Run(() => SendMails(contactFormVM));
    return Json("success");
  }
  return Json("failure");
}

public void SendMails(ContactFormViewModel contactFormVM) {
  domain.SendContactFormUserMail(contactFormVM);
  domain.SendContactFormAdminMail(contactFormVM);
}
like image 179
Steven Wexler Avatar answered Sep 27 '22 18:09

Steven Wexler