开发者

IHttpAsyncHandler and IObservable web requests

开发者 https://www.devze.com 2022-12-26 05:24 出处:网络
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 Th

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.IsN开发者_如何学JAVAullOrEmpty(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);
    }
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消