C# asp.net mvc 5异步操作方法

C# asp.net mvc 5异步操作方法,c#,asp.net,asp.net-mvc,asynchronous,C#,Asp.net,Asp.net Mvc,Asynchronous,我有以下带有async和wait关键字的操作方法: [HttpPost] public async Task<ActionResult> Save(ContactFormViewModel contactFormVM) { if (domain.SaveContactForm(contactFormVM) > 0)// saves data in database { bool result = await SendMails(contac

我有以下带有
async
wait
关键字的操作方法:

[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;
    }
[HttpPost]
公共异步任务保存(ContactFormViewModel contactFormVM)
{
如果(domain.SaveContactForm(contactFormVM)>0)//将数据保存到数据库中
{
bool result=wait SendMails(contactFormVM);//需要异步执行此方法,但它是同步执行的
返回Json(“成功”);
}
返回Json(“失败”);
}
公共异步任务发送邮件(ContactFormViewModel contactFormVM)
{
wait Task.Delay(0);//如何在此函数中使用wait关键字?
domain.SendContactFormUserMail(contactFormVM);
domain.SendContactFormAdminMail(contactFormVM);
返回true;
}
在上面的代码中,一旦数据库操作完成,我想立即返回
Json()
结果,然后调用应该在后台执行的
SendMails()
方法。我应该对上述代码进行哪些更改

等待运算符应用于异步方法中的任务,以暂停方法的执行,直到等待的任务完成。这项任务是正在进行的工作

听起来您不想等待SendMails的结果。将async和Wait视为使用异步API的工具。具体来说,能够“等待”一个“异步”任务的结果是非常有用的。但是,如果您不关心“异步”(例如SendMails)任务的结果,那么您就不需要“等待”结果(例如布尔值)

相反,您可以简单地使用调用异步任务

[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);
}
[HttpPost]
公共异步任务保存(ContactFormViewModel contactFormVM){
如果(domain.SaveContactForm(contactFormVM)>0){//将数据保存到数据库中
运行(()=>SendMails(contactFormVM));
返回Json(“成功”);
}
返回Json(“失败”);
}
公开作废发送邮件(ContactFormViewModel contactFormVM){
domain.SendContactFormUserMail(contactFormVM);
domain.SendContactFormAdminMail(contactFormVM);
}

您遇到了什么错误?@utility我没有遇到任何错误。
SendMails()
方法正在同步执行,而不是异步执行。应该在当前线程上调用
返回Json(“success”)
,并且
SendMails()
应该异步执行。这正是我所需要的,它似乎正在工作,但是在
SendContactFormUserMail()中
函数我正在使用
StreamReader
类从文本文件中读取电子邮件模板,但我得到的
对象引用未设置为对象的实例。
异常..听起来您的变量之一是
null
。对这个问题的回答可能会有所帮助:我正在使用
HttpContext.Current.Server.MapPath()
方法读取文本文件,并且在异步方法中该值为null…您的HttpContext.Current为null,因为调用异步任务将使用一个无法访问HttpContext.Current的工作线程。有几个问题可能会对你有所帮助。这是一个与此问题相关的问题/答案示例:使用
HostingEnvironment.ApplicationPhysicalPath解决了我的问题获取应用程序的物理路径,并在服务器上测试代码,现在运行速度非常快..感谢您的简单回答。。。