如何设置IHttpAsyncHandler?

时间:2011-05-20 22:53:28

标签: c# asynchronous ihttphandler ihttpasynchandler

我正在尝试设置异步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;
    }
}

1 个答案:

答案 0 :(得分:0)

去看看dnrtv上的节目188 http://www.dnrtv.com/default.aspx?showNum=188

这将引导您了解.NET 4中的最新异步以及下一个.NET版本中即将推出的功能。

您还可以选择在Asynch ctp上执行此操作,该许可用于商业用途。

这使得异步成为一种风格。