如何设置IHttpAsyncHandler

本文关键字:IHttpAsyncHandler 设置 何设置 | 更新日期: 2023-09-27 17:57:29

我正在尝试设置一个异步HttpHandler,但我不知道我是否走在了正确的轨道上。似乎没有太多的文档。我只想得到启动DoWork()方法async的请求。

有几件事我不确定:

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;
    }
}

如何设置IHttpAsyncHandler

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

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

您还可以选择在异步ctp上执行此操作,异步ctp已获得商业使用许可。

这使得异步成为一种很好的工作方式。

相关文章:
  • 没有找到相关文章