Error : An asynchronous operation cannot be started at this time


When I am trying to call async method in Class from controller, I am getting the error as below

An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>

How can I resolve this error, here is my Current code

HelperClass.SendEmailDataAsync(user.email, html, user.Fullname); //Controller call to class method


        public static void SendEmailDataAsync(string emailId, string body, string Fullname)
        {

            MailMessage Msg = new MailMessage();
            Msg.From = new MailAddress("email", "Name");
            Msg.Subject ="Name";
            Msg.To.Add(emailId);
            Msg.Body = body;
            Msg.IsBodyHtml = true;
            Msg.Priority = MailPriority.High;
            SendEmail(Msg, true);
            
        }

        public static async System.Threading.Tasks.Task SendEmail(System.Net.Mail.MailMessage m, Boolean Async)
        {

            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential("email", "Password");
            smtp.EnableSsl = true;
            smtp.Timeout = 20000;         
            await smtp.SendMailAsync(m);

        }

What can be the issue here, how to resolve? any idea?


Asked by:- jon
1
: 12264 At:- 12/6/2017 8:05:47 AM
C# async await asp.net mvc







1 Answers
profileImage Answered by:- pika

Your call and callee both method must be async in other to make that work, also make sure that your Controller method returns an async Task

So to make that work, your code must be

//Controller
public class ServiceController : Controller 
{
    public async Task<ActionResult> Index()
    {       
        await HelperClass.SendEmailDataAsync(user.email, html, user.Fullname); //make the method also async  
        return View();
    }
}

//Class Methods to call
        public static async System.Threading.Tasks.Task  SendEmailDataAsync(string emailId, string body, string Fullname)
        {

            MailMessage Msg = new MailMessage();
            Msg.From = new MailAddress("email", "Name");
            Msg.Subject ="Name";
            Msg.To.Add(emailId);
            Msg.Body = body;
            Msg.IsBodyHtml = true;
            Msg.Priority = MailPriority.High;
            await SendEmail(Msg, true);
            
        }

        public static async System.Threading.Tasks.Task SendEmail(System.Net.Mail.MailMessage m, Boolean Async)
        {

            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential("email", "Password");
            smtp.EnableSsl = true;
            smtp.Timeout = 20000;         
            await smtp.SendMailAsync(m);

        }

If you are on Web-forms , you need to set the Async attribute for the page. Info here.

2
At:- 12/7/2017 2:32:09 PM
Thank you, after making both the methods of type async & controller Method also of type async, it worked 0
By : jon - at :- 12/18/2017 3:17:37 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use