简体   繁体   English

如何动态设置异步页面指令,以便异步方法起作用

[英]How to set Async Page Directive Dynamically so Async Methods work

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. 但是我收到错误,因为我的页面在页面指令中没有Async =“ true”。

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 http://msdn.microsoft.com/zh-CN/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. 构建异步页面时要记住的最后一点是,不应启动借用ASP.NET使用的同一线程池的异步操作。 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. 例如,在页面的异步点调用ThreadPool.QueueUserWorkItem是适得其反的,因为该方法从线程池中提取,导致用于处理请求的净线程数为零。 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. 相比之下,通常认为调用框架中内置的异步方法(例如HttpWebRequest.BeginGetResponse和SqlCommand.BeginExecuteReader)是安全的,因为这些方法倾向于使用完成端口来实现异步行为。

Questions: 问题:

1) How can I update the page to be Async in my c# code? 1)如何在我的C#代码中将页面更新为异步?

2) If I can't what is the down side with forcing all my pages to be Async=true? 2)如果不能将所有页面强制设置为Async = true,会有什么不利之处?

3) Is there an even better way to thread my task without being "counterproductive"? 3)有没有更好的方法来线程我的任务而又不会“适得其反”?

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. 您可以使用Server.Transfer调用该页面,并在完成后将其重定向回所需的页面。

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. 最后,如果您发送的电子邮件太多,则在同步发送邮件时会失去性能,那么也许您应该创建Windows服务来发送实际的电子邮件。 Your ASP.NET page would queue a request to this service (through MSMQ, or WCF) to have the service send the email. 您的ASP.NET页将对该服务(通过MSMQ或WCF)的请求进行排队,以使该服务发送电子邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM