IHttpAsyncHandler 和 IObservable Web 请求

发布于 2024-08-28 01:20:49 字数 2074 浏览 4 评论 0原文

在异步处理程序中,我从 webrequest 创建一个 IObservable,它返回一个重定向字符串。

我正在订阅该可观察对象并调用 AsyncResult.CompleteCall() 但我被迫使用 Thread.Sleep(100) 才能执行它。而且并不是每次都有效。我很确定这是不正确的。能否请你照亮一些光。谢谢你!

    public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object state)
    {
        _context = context;
        _ar = new AsyncResult(cb, state);

        _tweet = context.Request["tweet"];
        string url = context.Request["url"];

        if(String.IsNullOrEmpty(_tweet) || String.IsNullOrEmpty(url))
        {
            DisplayError("<h2>Tweet or url cannot be empty</h2>");
            return _ar;
        }

        _oAuth = new oAuthTwitterRx();
        using (_oAuth.AuthorizationLinkGet().Subscribe(p =>
        {
            _context.Response.Redirect(p);
            _ar.CompleteCall();
        },
                exception => DisplayError("<h2>Unable to connect to twitter, please try again</h2>")
                ))


        return _ar;
    }

public class AsyncResult : IAsyncResult
{
    private AsyncCallback _cb;
    private object _state;
    private ManualResetEvent _event;
    private bool _completed = false;
    private object _lock = new object();

    public AsyncResult(AsyncCallback cb, object state)
    {
        _cb = cb;
        _state = state;
    }

    public Object AsyncState
    {
        get { return _state; }
    }

    public bool CompletedSynchronously
    {
        get { return false; }
    }

    public bool IsCompleted
    {
        get { return _completed; }
    }

    public WaitHandle AsyncWaitHandle
    {
        get
        {
            lock (_lock)
            {
                if (_event == null)
                    _event = new ManualResetEvent(IsCompleted);
                return _event;
            }
        }
    }

    public void CompleteCall()
    {
        lock (_lock)
        {
            _completed = true;
            if (_event != null)
                _event.Set();
        }

        if (_cb != null)
            _cb(this);
    }
}

Within Async handler I'm creating an IObservable from webrequest which returns a redirect string.

I'm subscribing to that observable and calling AsyncResult.CompleteCall() but I'm forced to use Thread.Sleep(100) in order to get it executed. And it doesn't work every time. I'm pretty sure this is not correct. Could you please shine some light. Thank you!

    public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object state)
    {
        _context = context;
        _ar = new AsyncResult(cb, state);

        _tweet = context.Request["tweet"];
        string url = context.Request["url"];

        if(String.IsNullOrEmpty(_tweet) || String.IsNullOrEmpty(url))
        {
            DisplayError("<h2>Tweet or url cannot be empty</h2>");
            return _ar;
        }

        _oAuth = new oAuthTwitterRx();
        using (_oAuth.AuthorizationLinkGet().Subscribe(p =>
        {
            _context.Response.Redirect(p);
            _ar.CompleteCall();
        },
                exception => DisplayError("<h2>Unable to connect to twitter, please try again</h2>")
                ))


        return _ar;
    }

public class AsyncResult : IAsyncResult
{
    private AsyncCallback _cb;
    private object _state;
    private ManualResetEvent _event;
    private bool _completed = false;
    private object _lock = new object();

    public AsyncResult(AsyncCallback cb, object state)
    {
        _cb = cb;
        _state = state;
    }

    public Object AsyncState
    {
        get { return _state; }
    }

    public bool CompletedSynchronously
    {
        get { return false; }
    }

    public bool IsCompleted
    {
        get { return _completed; }
    }

    public WaitHandle AsyncWaitHandle
    {
        get
        {
            lock (_lock)
            {
                if (_event == null)
                    _event = new ManualResetEvent(IsCompleted);
                return _event;
            }
        }
    }

    public void CompleteCall()
    {
        lock (_lock)
        {
            _completed = true;
            if (_event != null)
                _event.Set();
        }

        if (_cb != null)
            _cb(this);
    }
}

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

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

发布评论

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

评论(1

苏佲洛 2024-09-04 01:20:49

迟到总比不做好;我认为以下文章准确地解释了您想要实现的目标: http://blogs.msdn.com/b/jeffva/archive/2010/09/15/rx-on-the-server -n-observable-asp-net.aspx 的第 5 部分

Better late then never; I think the following article explains exactly what you try to achieve: http://blogs.msdn.com/b/jeffva/archive/2010/09/15/rx-on-the-server-part-5-of-n-observable-asp-net.aspx

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