How to setup an IHttpAsyncHandler?
I am trying to setup an asynchronous HttpHandler, but I have no idea if I am on the right track. There doesn't seem to be much documentation on it. I would just like to get the request to fire off the DoWork() method async.
A few things I am not sure about:
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 NotImplementedExcept开发者_如何转开发ion(); }
}
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;
}
}
Go and watch show 188 on dnrtv http://www.dnrtv.com/default.aspx?showNum=188
This will walk you through state of the art asynchrony within .NET 4 and the upcoming features in the next .NET release.
You will also have the option of doing this on the Asynch ctp, which is licensed for commercial use.
This makes asynchrony a blast to work with.
精彩评论