问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/10250042/does-mvcmailer-sendasync-block-a-asp-net-mvc-request