2

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

1 回答 1

0

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

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

您还可以选择在授权用于商业用途的异步 ctp 上执行此操作。

这使得异步成为一个爆炸性的工作。

于 2011-07-31T16:58:23.407 回答