我正在尝试设置一个异步 HttpHandler,但我不知道我是否走在正确的轨道上。似乎没有太多关于它的文档。我只想获得异步触发 DoWork() 方法的请求。
一些我不确定的事情:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
public class WebServiceHandlerAsync : IHttpAsyncHandler
{
/// <summary>
/// BeingProcessRequest
/// </summary>
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
{
context.Response.Output.WriteLine("Starting");
context.Response.Flush();
// Invokes the BeginProcessRequest method on the asynchronous HTTP handler
RequestResult result = new RequestResult(context, null, extraData);
result.DoWork();
return result;
}
/// <summary>
/// Do on end of request (blocking)
/// </summary>
public void EndProcessRequest(IAsyncResult result)
{
}
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
throw new NotImplementedException();
}
}
/// <summary>
/// Async object
/// </summary>
public class RequestResult : IAsyncResult
{
public HttpContext Context { get; private set; }
private bool _completed;
private object _extraData;
public object AsyncState
{
get { return _extraData; }
}
public System.Threading.WaitHandle AsyncWaitHandle
{
get { throw new NotImplementedException(); }
}
public bool CompletedSynchronously
{
get { return false; }
}
public bool IsCompleted
{
get { return _completed; }
}
/// <summary>
/// Constructor
/// </summary>
public RequestResult(HttpContext context, AsyncCallback callback, object extraData)
{
Context = context;
_extraData = extraData;
}
/// <summary>
/// Handle request
/// </summary>
public void DoWork()
{
// do some work
Context.Response.Output.WriteLine("Working...");
_completed = true;
}
}