如何动态设置异步页面指令以使异步方法起作用
我正在编写一些实用程序代码来异步发送电子邮件。
var mailClient = new SmtpClient(smtpHost);
mailClient.SendCompleted += new SendCompletedEventHandler(mailClient_SendCompleted);
using (var mailMessage = new MailMessage())
{
if (!((System.Web.UI.Page)HttpContext.Current.CurrentHandler).IsAsync)
{
// set to Async????
}
mailClient.SendAsync(mailMessage, new { EmailID });
}
但我收到错误,因为我的页面在页面指令中没有 Async="true"。
这是您得到的标准错误:
“在此上下文中不允许异步操作。页面启动 异步操作必须将 Async 属性设置为 true 并且 异步操作只能在之前的页面上启动 PreRenderComplete 事件。”
我读到了这个:(最后一段)
http://msdn.microsoft.com/ en-us/magazine/cc163725.aspx
最后一点要记住 构建异步页面就是你 不应该启动异步 借用相同的操作 ASP.NET 使用的线程池。 为了 例如,调用 ThreadPool.QueueUserWorkItem 位于 页面的异步点是 因为这种方法适得其反 从线程池中提取,结果 净增益为零线程 处理请求。 相比之下, 调用内置的异步方法 进入框架,方法如 HttpWebRequest.BeginGetResponse 和 SqlCommand.BeginExecuteReader 是 一般认为是安全的 因为这些方法往往使用 要实施的完成端口 异步行为。
问题:
1) 如何在我的 C# 代码中将页面更新为异步?
2) 如果我不能,那么强制所有的缺点是什么?我的页面要 Async=true?
3) 有没有更好的方法来线程化我的任务而不“适得其反”?
I am writing some Utility code to send off emails Async.
var mailClient = new SmtpClient(smtpHost);
mailClient.SendCompleted += new SendCompletedEventHandler(mailClient_SendCompleted);
using (var mailMessage = new MailMessage())
{
if (!((System.Web.UI.Page)HttpContext.Current.CurrentHandler).IsAsync)
{
// set to Async????
}
mailClient.SendAsync(mailMessage, new { EmailID });
}
But I get errors because my Pages don't have Async="true" in the page directives.
here is the standard error that you get:
"Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true and an asynchronous operation can only be started on a page prior to PreRenderComplete event."
I read this: (last paragraph )
http://msdn.microsoft.com/en-us/magazine/cc163725.aspx
A final point to keep in mind as you
build asynchronous pages is that you
should not launch asynchronous
operations that borrow from the same
thread pool that ASP.NET uses. For
example, calling
ThreadPool.QueueUserWorkItem at a
page's asynchronous point is
counterproductive because that method
draws from the thread pool, resulting
in a net gain of zero threads for
processing requests. By contrast,
calling asynchronous methods built
into the Framework, methods such as
HttpWebRequest.BeginGetResponse and
SqlCommand.BeginExecuteReader, is
generally considered to be safe
because those methods tend to use
completion ports to implement
asynchronous behavior.
Questions:
1) How can I update the page to be Async in my c# code?
2) If I can't what is the down side with forcing all my pages to be Async=true?
3) Is there an even better way to thread my task without being "counterproductive"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要从多少个不同的页面发送邮件?
另外,当您尝试发送异步时遇到什么错误? 请编辑您的问题以包含整个异常。
考虑创建一个(异步)页面来发送电子邮件。 您可以使用 Server.Transfer 调用该页面,并在完成后将其重定向回您想要的页面。
最后,如果您发送的电子邮件太多,导致同步发送邮件时性能下降,那么也许您应该创建一个 Windows 服务来发送实际的电子邮件。 您的 ASP.NET 页面会将对此服务的请求排队(通过 MSMQ 或 WCF)以使该服务发送电子邮件。
How many different pages do you need to send mail from?
Also, what error did you get when you tried to send async? Please edit your question to contain the entire exception.
Consider creating a single (async) page to send email from. You can call that page by using Server.Transfer, and have it redirect back to your desired page when done.
Finally, if you're sending so many emails that you lose performance when sending mail synchronously, then perhaps you should create a Windows Service to send the actual email. Your ASP.NET page would queue a request to this service (through MSMQ, or WCF) to have the service send the email.