4

(只能使用 .NET 3.5 stock,所以没有任务,没有响应式扩展)

我有,我认为这是一个简单的案例,但我对此感到困惑。

简而言之,我将 BeginGetRequestStream 的 IAsyncResult 返回给 BeginMyOperation() 的调用者,并且我想真正将 BeginGetResponse 的 IAsyncResult 发回,它在调用 EndGetRequestStream 时被调用。

所以我想知道,我该怎么做

      public IAsyncResult BeginMyOperation(...)
      {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(requestUri);
            webRequest.Method = "POST";

            // This is the part, that puzzles me. I don't want to send this IAsyncResult back.
            return webRequest.BeginGetRequestStream(this.UploadingStreamCallback, state);
       }

      // Only want this to be called when the EndGetResponse is ready.
      public void EndMyOperation(IAsyncResult ar)
      {

      }

      private IAsyncResult UploadingStreamCallback(IAsyncResult asyncResult)
      {
            using (var s = state.WebRequest.EndGetRequestStream(asyncResult))
            {
                using (var r = new BinaryReader(state.Request.RequestData))
                {
                    byte[] uploadBuffer = new byte[UploadBufferSize];
                    int bytesRead;
                    do
                    {
                        bytesRead = r.Read(uploadBuffer, 0, UploadBufferSize);

                        if (bytesRead > 0)
                        {
                            s.Write(uploadBuffer, 0, bytesRead);
                        }
                    }
                    while (bytesRead > 0);
                }
            }

            // I really want to return this IAsyncResult to the caller of BeginMyOperation
            return state.WebRequest.BeginGetResponse(new AsyncCallback(state.Callback), state);
        }
4

5 回答 5

3

我认为解决这个问题的最简单方法是使用Task包装器。特别是,您可以在完成TaskCompletionSourceBeginGetResponse完成。然后就返回Task那个TaskCompletionSource。请注意Taskimplements IAsyncResult,因此您的客户端代码不必更改。

就个人而言,我会更进一步:

  1. 包裹BeginGetRequestStream在一个Task(使用FromAsync)。
  2. Task为处理请求创建一个延续并包装BeginGetResponse在一个Task(再次,使用FromAsync)中。
  3. Task完成TaskCompletionSource.

恕我直言,异常和结果值Task比 s更自然地处理IAsyncResult

于 2011-04-04T04:09:40.123 回答
2

您尝试做的事情是可行的,但您需要创建一个新的 IAsyncResult 实现(类似于“CompositeResult”,它监视第一个 IAsyncResult,然后启动第二个调用)。

然而,使用 Reactive Extensions 这个任务实际上要容易得多 - 在这种情况下,您将使用 Observable.FromAsyncPattern 将您的 Begin/End 方法转换为返回 IObservable(表示异步结果)的 Func,然后使用 SelectMany 链接它们:

IObservable<Stream> GetRequestStream(string Url);
IObservable<bool> MyOperation(Stream stream);

GetRequestStream().SelectMany(x => MyOperation(x)).Subscribe(x => {
    // When everything is finished, this code will run
});
于 2011-04-04T04:00:28.060 回答
2

我意识到这个问题已经快一年了,但如果提问者的限制仍然存在,那么 .NET 3.5 上有一个选项可以轻松组合异步操作。查看 Jeff Richter 的PowerThreading 库。在Wintellect.PowerThreading.AsyncProgModel命名空间中,您会发现AsyncEnumerator该类的几个变体,您可以将它们与序列生成器一起使用来编写异步代码,就好像它是顺序的一样。

它的要点是您将异步代码编写为返回 a 的序列生成器的主体IEnumerator<int>,并且每当您调用异步方法时,您都会发出 ayield return等待异步操作的数量。图书馆处理血腥的细节。

例如,将一些数据发布到 url 并返回结果的内容:

public IAsyncResult BeginPostData(string url, string content, AsyncCallback callback, object state)
{
    var ae = new AsyncEnumerator<string>();
    return ae.BeginExecute(PostData(ae, url, content), callback, state);
}

public string EndPostData(IAsyncResult result)
{
    var ae = AsyncEnumerator<string>.FromAsyncResult(result);
    return ae.EndExecute(result);
}

private IEnumerator<int> PostData(AsyncEnumerator<string> ae, string url, string content)
{
    var req = (HttpWebRequest)WebRequest.Create(url);
    req.Method = "POST";

    req.BeginGetRequestStream(ae.End(), null);
    yield return 1;

    using (var requestStream = req.EndGetRequestStream(ae.DequeAsyncResult()))
    {
        var bytes = Encoding.UTF8.GetBytes(content);
        requestStream.BeginWrite(bytes, 0, bytes.Length, ae.end(), null);
        yield return 1;

        requestStream.EndWrite(ae.DequeueAsyncResult());
    }

    req.BeginGetResponse(ae.End(), null);
    yield return 1;

    using (var response = req.EndGetResponse(ae.DequeueAsyncResult()))
    using (var responseStream = response.GetResponseStream())
    using (var reader = new StreamReader(responseStream))
    {
        ae.Result = reader.ReadToEnd();
    }
}

如您所见,私有PostData()方法负责大部分工作。如三个yield return 1语句所示,启动了三个异步方法。使用这种模式,您可以链接任意数量的异步方法,并且仍然只返回一个IAsyncResult给调用者。

于 2012-04-16T01:17:49.543 回答
1

我真的不明白你想要实现什么,但我认为你应该重新考虑代码。IAsyncResult 实例是允许处理异步方法调用的对象,它们是在您通过BeginXXX执行异步调用时创建的。

在您的示例中,您基本上想要返回一个它不存在的 IAsyncResult 实例。

我真的不知道您要解决哪个问题,但也许其中一种方法对您更有效:

  1. 将此代码封装在一个类中,并让您的代码的用户知道操作是通过订阅事件完成的。
  2. 将此代码封装在一个类中,并让用户提供一个回调委托,该委托将在工作完成时调用。您可以将结果作为参数传递给此回调

希望能帮助到你!

于 2011-04-04T03:29:34.237 回答
0

首先,从 Jeffrey Richter 的 MSDN 杂志文章“实现 CLR 异步编程模型(2007 年 3 月号)”AsyncResultNoResult中获取实现代码。AsyncResult<TResult>

一旦有了这些基类,就可以相对轻松地实现自己的异步结果。在此示例中,我将使用您的基本代码启动 Web 请求,然后将响应作为由多个内部异步操作组成的单个异步操作来获取。

// This is the class that implements the async operations that the caller will see
internal class MyClass
{
    public MyClass() { /* . . . */ }

    public IAsyncResult BeginMyOperation(Uri requestUri, AsyncCallback callback, object state)
    {
        return new MyOperationAsyncResult(this, requestUri, callback, state);
    }

    public WebResponse EndMyOperation(IAsyncResult result)
    {
        MyOperationAsyncResult asyncResult = (MyOperationAsyncResult)result;
        return asyncResult.EndInvoke();
    }

    private sealed class MyOperationAsyncResult : AsyncResult<WebResponse>
    {
        private readonly MyClass parent;
        private readonly HttpWebRequest webRequest;
        private bool everCompletedAsync;

        public MyOperationAsyncResult(MyClass parent, Uri requestUri, AsyncCallback callback, object state)
            : base(callback, state)
        {
            // Occasionally it is necessary to access the outer class instance from this inner
            // async result class.  This also ensures that the async result instance is rooted
            // to the parent and doesn't get garbage collected unexpectedly.
            this.parent = parent;

            // Start first async operation here
            this.webRequest = WebRequest.Create(requestUri);
            this.webRequest.Method = "POST";
            this.webRequest.BeginGetRequestStream(this.OnGetRequestStreamComplete, null);
        }

        private void SetCompletionStatus(IAsyncResult result)
        {
            // Check to see if we did not complete sync. If any async operation in
            // the chain completed asynchronously, it means we had to do a thread switch
            // and the callback is being invoked outside the starting thread.
            if (!result.CompletedSynchronously)
            {
                this.everCompletedAsync = true;
            }
        }

        private void OnGetRequestStreamComplete(IAsyncResult result)
        {
            this.SetCompletionStatus(result);
            Stream requestStream = null;
            try
            {
                stream = this.webRequest.EndGetRequestStream(result);
            }
            catch (WebException e)
            {
                // Cannot let exception bubble up here as we are on a callback thread;
                // in this case, complete the entire async result with an exception so
                // that the caller gets it back when they call EndXxx.
                this.SetAsCompleted(e, !this.everCompletedAsync);
            }

            if (requestStream != null)
            {
                this.WriteToRequestStream();
                this.StartGetResponse();
            }
        }

        private void WriteToRequestStream(Stream requestStream) { /* omitted */ }

        private void StartGetResponse()
        {
            try
            {
                this.webRequest.BeginGetResponse(this.OnGetResponseComplete, null);
            }
            catch (WebException e)
            {
                // As above, we cannot let this exception bubble up
                this.SetAsCompleted(e, !this.everCompletedAsync);
            }
        }

        private void OnGetResponseComplete(IAsyncResult result)
        {
            this.SetCompletionStatus(result);
            try
            {
                WebResponse response = this.webRequest.EndGetResponse(result);

                // At this point, we can complete the whole operation which
                // will invoke the callback passed in at the very beginning
                // in the constructor.
                this.SetAsCompleted(response, !this.everCompletedAsync);
            }
            catch (WebException e)
            {
                // As above, we cannot let this exception bubble up
                this.SetAsCompleted(e, !this.everCompletedAsync);
            }
        }
    }
}

需要注意的一些事项:

  • 您不能在异步回调的上下文中引发异常。您将崩溃您的应用程序,因为没有人来处理它。相反,始终在异常情况下完成异步操作。这保证了调用者将看到 EndXxx 调用的异常,然后可以适当地处理它。
  • 假设 BeginXxx 可以抛出的任何东西也可能从 EndXxx 中抛出。上面的示例假设 WebException 在任何一种情况下都可能发生。
  • 在调用者执行异步循环的情况下,设置“同步完成”状态很重要。这将在调用者需要从异步回调中返回时通知调用者,以避免“堆栈潜水”。有关这方面的更多信息,请参阅 Michael Marucheck 的博客文章“ Indigo 中的异步编程”(请参阅​​ Stack Dive 部分)。

异步编程不是最简单的事情,但是一旦你理解了这些概念,它就会非常强大。

于 2011-04-23T16:45:20.550 回答