简体   繁体   English

BeginGetResponse回调中的WP7 Propogate异常

[英]WP7 Propogate exception from BeginGetResponse callback

I'm using HttpWebRequest to call a web service. 我正在使用HttpWebRequest调用Web服务。 If the AsyncCallback from BeginGetResponse throws an error, I want to propagate it up to my main program flow. 如果BeginGetResponse的AsyncCallback引发错误,我想将其传播到我的主程序流。 I'm having trouble doing this because the error doesn't get propagated beyond the AsyncCallback. 我在执行此操作时遇到了麻烦,因为该错误不会传播到AsyncCallback之外。 I've tried putting try/catch blocks at each step of the HttpWebRequest chain, but it never propagates beyond the "ResponseCallBack" method. 我已经尝试在HttpWebRequest链的每个步骤中放置try / catch块,但是它从未传播过“ ResponseCallBack”方法。 Is it possible to get it back to the main thread? 是否可以将其返回到主线程?

private void StartRequest()
{
    // Code to create request object is here
    // ...

    httpRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), httpRequest);
}

private void GetRequestStreamCallback(IAsyncResult result)
{
    HttpWebRequest request = (HttpWebRequest)result.AsyncState;

    // End the operation
    var postStream = request.EndGetRequestStream(result);
    string body = GenerateRequestBody();

    // Convert the string into a byte array
    byte[] postBytes = Encoding.UTF8.GetBytes(body);

    // Write to request stream
    postStream.Write(postBytes, 0, postBytes.Length);
    postStream.Close();

    // Start the asynchronous operation to get the resonse
    try
    {
        request.BeginGetResponse(new AsyncCallback(ResponseCallback), request);
    }
    catch (Exception)
    {
        throw;
    }
}

private void ResponseCallback(IAsyncResult result)
{
    string contents = String.Empty;
    HttpWebRequest request = (HttpWebRequest)result.AsyncState;
    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);

    using (Stream stream = response.GetResponseStream())
    using (StreamReader reader = new StreamReader(stream))
    {
        contents = reader.ReadToEnd();
    }

    // Check the status
    if (response.StatusCode == HttpStatusCode.OK)
    {
        //EXCEPTION NEVER MAKES IT PASSED HERE, EVEN IF I HAVE IT IN A TRY/CATCH BLOCK AND RE-THROW IT.
        _object = ProcessResponseEntity(contents);
    }
}

I think you're getting confused about the way asynchronous code exectution works and how the callback execution fits in with the calling code. 我认为您对异步代码执行的工作方式以及回调执行与调用代码的配合方式感到困惑。

Within GetRequestStreamCallback , after the call to request.BeginGetResponse the method will continue to execute and in your example just end. GetRequestStreamCallback ,在调用request.BeginGetResponse之后,该方法将继续执行,在您的示例中将结束。

It is not known when (or even if) ResponseCallback will execute or what will be happening on the UI thread when it does. 还不知道什么时候(或什至) ResponseCallback会执行,或者执行时UI线程会发生什么。 Because of this, ResponseCallback will execute on a different thread. 因此, ResponseCallback将在不同的线程上执行。

It's possible to have code within the callback run on the UI thread (which you'll need to do you want to interact with the UI) by using Dispatcher.BeginInvoke . 通过使用Dispatcher.BeginInvoke ,可以在UI线程上运行回调(您需要与UI进行交互)中的代码。 However you can't have this execute within the context of another method. 但是,您不能在另一个方法的上下文中执行此操作。

While I wouldn't recommend it, you may want to have a look at this discussion on making the callback appear to execute synchronously. 虽然我不建议这样做,但您可能希望看一下有关使回调看起来像是同步执行的讨论 This will block your UI thread though and so is NOT recommended. 但是,这将阻止您的UI线程,因此不建议这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM