为什么是C#.NET WebException未在WP7仿真程序中捕获

本文关键字:仿真程序 WP7 未在 NET WebException 为什么 | 更新日期: 2023-09-27 17:58:10

我正在开发一个Windows Phone 7应用程序,我需要在我们公司的REST api上放入一些数据。

我正在测试这个API调用,遇到了一个无法捕获的WebException。我的调用最初是在lambda中进行的,所以我把它拉到了成员函数中,但仍然没有成功。

错误字符串为"远程服务器返回错误:NotFound"。这并不意外,我的应用程序尝试的URI可能无效,因为我搞砸了,或者因为我们的临时服务器坏了。

如果我不能捕捉到这个异常,我该如何检查我的请求是否失败?

这是我的玩具示例:

void TriggerApiCall()
{
    var request = HttpWebRequest.Create(new Uri(
        "http://api.ourcompany.com" +
        "/stuff/" +
        someParameterizedPath + "?" +
        "firstParam=" + someClassMember +
        "&secondParam=8badf00d"));
    request.Method = "PUT";
    request.BeginGetResponse(TryCall, request);
}
private void TryCall(IAsyncResult result)
{
    HttpWebRequest request = result.AsyncState as HttpWebRequest;
    var completed = result.IsCompleted;
    WebResponse response = null;
    try {
        bool hasresp = request.HaveResponse;
        response = request.EndGetResponse(result);
    } catch (Exception e) {
        System.Diagnostics.Debug.WriteLine(e); // Never reaches here,
                                               // Exception propagates to top
    }
    response.Close();
}

为什么是C#.NET WebException未在WP7仿真程序中捕获

Research告诉我在Silverlight3及更高版本中有一个新的ClientHttp堆栈。

http://msdn.microsoft.com/en-us/library/dd920295(v=vs.95).aspx