We're sending an email out when a particular MVC controller method is called. The code is as below. When benchmarking the AJAX response time inside a browser we see that the client.SendAsync(mail, null)
and client.Send(mail)
implementations BOTH take about the same time i.e. 1.8 seconds as compared to not sending the mail at all.
当调用特定的MVC控制器方法时,我们会发送一封电子邮件。代码如下。在浏览器内对AJAX响应时间进行基准测试时,我们发现client.SendAsync(mail,null)和client.Send(mail)实现大约需要相同的时间,即完全不发送邮件的时间为1.8秒。
public ActionResult jsonPosted(int? id, SomeDataClass data)
{
...
// NOTE: remode 'using' statement for Async calls
// else no email ever sees light
using (SmtpClient client = new SmtpClient())
{
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("name@server.com", "password");
client.Port = 587;
client.Host = "smtp.server.com";
client.EnableSsl = true;
MailMessage mail = new MailMessage();
mail.From = new MailAddress("admin@server.com");
mail.To.Add("sales@server.com");
mail.Subject = "Coming soon: user " + newUser.Email + " registered on list";
mail.Body = "Email address is : " + newUser.Email + "\n";
mail.Body += "Date of signup is : " + newUser.SignupTime.ToString();
try
{
// Both take about 1.9 seconds ?!?!
//client.SendAsync(mail, null);
client.Send(mail);
}
catch (SmtpException e)
{
// bad smtp, no mail for you
}
}
...
return Json(dataIndependentOfEmail);
}
Question:
- Why do the two methods take the same time? Async should return immediately while Sync should block for the entire processing time, right? As I understand Async consumes another thread and THAT thread blocks during work. However at least the user on the 1st thread should see a performance boost.
- How does one properly use
using
to dispose off classes likeSmtpClient
andMailMessage
in async mode?
为什么这两种方法需要同时进行?异步应该立即返回,而Sync应该阻止整个处理时间,对吗?据我所知,Async在工作期间消耗了另一个线程和那个线程阻塞。但是至少第一个线程上的用户应该看到性能提升。
如何正确使用在异步模式下处理类SmtpClient和MailMessage等类?
1 个解决方案
#1
0
See here for an msdn example on the async smtpclient.
In this example they are not using dispose as (and this is a minor guess) the using will wait on the async operation to complete.
For testing purposes try removing the using statement, i expect the sendAsync to be faster then the send.
Offcourse you're not dispoing the smtpclient object properly, one way to handle the disposing is to create a new thread which handles the email the old skool way. You wouldn't have the cleanness of SendAssync but at least you're sure that the user gets instant feedback + the smtpclient is disposed properly.
Edit: an even better solution is to use the event sendcompleted and dispose the smtpclient there.
see here for an example
有关async smtpclient的msdn示例,请参见此处。在这个例子中,他们没有使用dispose(这是一个小猜测),using将等待异步操作完成。出于测试目的尝试删除using语句,我希望sendAsync比发送更快。当然你没有正确地处理smtpclient对象,处理处理的一种方法是创建一个新线程来处理旧skool方式的电子邮件。你不会有SendAssync的清洁,但至少你确定用户得到即时反馈+ smtpclient正确处理。编辑:更好的解决方案是使用事件sendcompleted并在那里配置smtpclient。在这里看一个例子
#1
0
See here for an msdn example on the async smtpclient.
In this example they are not using dispose as (and this is a minor guess) the using will wait on the async operation to complete.
For testing purposes try removing the using statement, i expect the sendAsync to be faster then the send.
Offcourse you're not dispoing the smtpclient object properly, one way to handle the disposing is to create a new thread which handles the email the old skool way. You wouldn't have the cleanness of SendAssync but at least you're sure that the user gets instant feedback + the smtpclient is disposed properly.
Edit: an even better solution is to use the event sendcompleted and dispose the smtpclient there.
see here for an example
有关async smtpclient的msdn示例,请参见此处。在这个例子中,他们没有使用dispose(这是一个小猜测),using将等待异步操作完成。出于测试目的尝试删除using语句,我希望sendAsync比发送更快。当然你没有正确地处理smtpclient对象,处理处理的一种方法是创建一个新线程来处理旧skool方式的电子邮件。你不会有SendAssync的清洁,但至少你确定用户得到即时反馈+ smtpclient正确处理。编辑:更好的解决方案是使用事件sendcompleted并在那里配置smtpclient。在这里看一个例子