我有一个异步操作,由于各种原因,需要使用对ASP.NET网页的HTTP调用来触发该操作.当我的页面被请求时,它应该启动此操作,并立即向客户端返回确认.
该方法也是通过WCF Web服务公开的,它可以完美地工作.
在我第一次try 时,抛出了一个异常,告诉我:
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.
当然,我在@Page
指令中添加了Async="true"
参数.现在,我没有收到任何错误,但是在异步操作完成之前,页面一直处于阻塞状态.
我怎样才能得到一个真正的火焰而忘记页面工作?
Edit:一些代码以获取更多信息.这比这要复杂一点,但我已经试着把大概的 idea 写进go 了.
public partial class SendMessagePage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string message = Request.QueryString["Message"];
string clientId = Request.QueryString["ClientId"];
AsyncMessageSender sender = new AsyncMessageSender(clientId, message);
sender.Start();
Response.Write("Success");
}
}
AsyncMessageSender类:
public class AsyncMessageSender
{
private BackgroundWorker backgroundWorker;
private string client;
private string msg;
public AsyncMessageSender(string clientId, string message)
{
this.client = clientId;
this.msg = message;
// setup background thread to listen
backgroundThread = new BackgroundWorker();
backgroundThread.WorkerSupportsCancellation = true;
backgroundThread.DoWork += new DoWorkEventHandler(backgroundThread_DoWork);
}
public void Start()
{
backgroundThread.RunWorkerAsync();
}
...
// after that it's pretty predictable
}