gpt4 book ai didi

c# - IHttpAsyncHandler 和 IObservable 网络请求

转载 作者:太空宇宙 更新时间:2023-11-03 14:30:58 27 4
gpt4 key购买 nike

在异步处理程序中,我从 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);
}
}

最佳答案

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

关于c# - IHttpAsyncHandler 和 IObservable 网络请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2596398/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com