0

我想尽快访问大量(100k+)的 JSON 文件,将它们序列化,并存储请求的 HTTP 响应状态代码(无论是成功还是失败)。(我正在使用System.Runtime.Serialization.JsonDataContract)。我打算对状态代码和序列化对象做进一步的工作,但作为一个测试平台,我有这段代码:

List<int> ids = new List<int>();
for (int i = MIN; i < MAX; i++)
    ids.Add(i);
var tasks = ids.Select(id =>
{
var request = WebRequest.Create(GetURL(id));
return Task
    .Factory
    .FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, id)
    .ContinueWith(t =>
    {
    HttpStatusCode code = HttpStatusCode.OK;
    Item item = null;
    try
    {
        using (var stream = t.Result.GetResponseStream())
        {
        DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Item));
        item = ((Item)jsonSerializer.ReadObject(stream));
        }
    }
    catch (AggregateException ex)
    {
        if (ex.InnerException is WebException)
        code = ((HttpWebResponse)((WebException)ex.InnerException).Response).StatusCode;
    }

    });
}).ToArray();
Task.WaitAll(tasks);

使用这种方法,我能够比我以前使用的同步方法更快地处理文件。

虽然,我知道当状态码是或时GetResponseStream()会抛出。因此,要捕获这些状态代码,我需要捕获此异常。但是,在此 TPL 的上下文中,它嵌套在 an on an中。这使得这条线真的很混乱:WebException4xx5xxInnerExceptionAggregateException

    code = ((HttpWebResponse)((WebException)ex.InnerException).Response).StatusCode;

虽然,这行得通......我想知道在这种情况下是否有更好/更清晰的方法来捕获这样的异常?

4

2 回答 2

2

看看 MSDN 文章:异常处理(任务并行库)

例如,您可能希望按如下方式重写代码:

try
{
    using (var stream = t.Result.GetResponseStream())
    {
        DataContractJsonSerializer jsonSerializer = new
            DataContractJsonSerializer(typeof(Item));

        item = ((Item)jsonSerializer.ReadObject(stream));
    }
}
catch (AggregateException ex)
{
    foreach (var e in ex.InnerExceptions)
    {
        bool isHandled = false;
        if (e is WebException)
        {
            WebException webException = (WebException)e;
            HttpWebResponse response = webException.Response as HttpWebResponse;
            if (response != null)
            {
                code = response.StatusCode;
                isHandled = true;
            }
        }

        if (!isHandled)
            throw;
    }
}
于 2012-10-16T15:40:43.680 回答
1

试穿这个尺寸。GetBaseException 返回导致问题的异常。

try
{
}
catch (System.AggregateException aex)
{

    var baseEx = aex.GetBaseException() as WebException;
    if (baseEx != null)
    {
        var httpWebResp = baseEx.Response as HttpWebResponse;
        if (httpWebResp != null)
        {
            var code = httpWebResp.StatusCode;
            // Handle it...
        }                    
    }

    throw;
}
于 2012-10-16T15:51:53.587 回答