如何设置 IHttpAsyncHandler?

发布于 2024-11-09 07:48:08 字数 2174 浏览 6 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难忘№最初的完美 2024-11-16 07:48:08

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

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

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

这使得异步变得非常容易使用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文