Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does MvcMailer SendAsync block a ASP.NET MVC Request?

Does anyone know if the MvcMailer SendAsync method in SmtpClientWrapper blocks a ASP.NET MVC Request? Looking at the MvcMailer wiki and the code I would say yes.

So I would still need to use something like WebBackgrounder as disussed here for a safe and truly asynchronous mail send in my MVC app?

I'm looking for a definitive answer thanks.

like image 805
DalSoft Avatar asked Apr 20 '12 16:04

DalSoft


1 Answers

After downloading MvcMailer and running my own tests I can confirm that SendAsync does block a ASP.NET request until completion.

Microsoft confirms this behaviour https://connect.microsoft.com/VisualStudio/feedback/details/688210/smtpclient-sendasync-blocking-my-asp-net-mvc-request

"SendAsync() calls SynchronizationContext.OperationStarted(), which is a cue to not dispose of the HttpContext instance (or even progress the request) until the asynchronous operation has completed."

As MvcMailer just wraps SendAsync from System.Net.Mail it suffers from the same limitations.

The correct way to send email asynchronously is to use something like WebBackgrounder, as then it is a totally background operation (so it doesn't matter if you use SendAsync or Send).

To keep things simple you could also use Ajax to send the email, but this has the disadvantage of being a client rather than a server operation.

like image 187
DalSoft Avatar answered Oct 29 '22 17:10

DalSoft