Reputation: 907
I have an MVC controller which is called with an Ajax post to send an email. I have been trying to use the async/await pattern but am finding that the code below causes a significant delay on the SendMailAsync(email) method. Can anyone explain why this may be the case?
(Note, smtpClient is set in my webconfig of my UI project)
[HttpPost]
public async Task TestMethod()
{
Workflow.Mailer rm = new Workflow.Mailer();
await rm.TestEmailBasic();
}
public async Task TestEmailBasic()
{
var email = new MailMessage("[email protected]", "[email protected]", "Testing basic email", "Hello world");
await SendEmailAsync(email);
}
public async Task SendEmailAsync(MailMessage email)
{
using (var smtpClient = new SmtpClient())
{
email.Subject = "Async test";
await smtpClient.SendMailAsync(email);
}
}
Upvotes: 1
Views: 2093
Reputation: 456322
As I explain on my blog, async doesn't change the HTTP protocol. An await
in an ASP.NET request "yields" to the ASP.NET threadpool, not to the browser. You can return early or fire-and-forget but those approaches can be dangerous because ASP.NET isn't designed for it.
Upvotes: 2