在GetAsync上使用await时,HttpClient不会抛出异常

时间:2013-04-08 17:40:37

标签: c# task-parallel-library async-await

我正在使用以下代码获取端点并将其写入缓存:

public async Task UpdateCacheFromHttp(string Uri)
{
    if (string.IsNullOrEmpty(Uri))
        return;

    var httpClient = new HttpClient();
    var response = await httpClient.GetAsync(Uri);

    if ((response != null) && (response.IsSuccessStatusCode))
    {
        var responseStream = await response.Content.ReadAsStreamAsync();
        WriteToCache(responseStream);
    }
}

代码在IIS上运行。

如果无法访问端点,我希望GetAsync抛出异常。即使使用Try-Catch,它也似乎永远不会失败。 GetAsync永远不会返回(我在HttpClient上尝试了5秒的超时,仍然没有返回)。

这会引发异常:

public Task UpdateCacheFromHttp(string Uri)
{
    var updateCacheTask = Task.Factory.StartNew(new Action(() =>
    {
        if (string.IsNullOrEmpty(Uri))
            return;

        var httpClient = new HttpClient();
        var response = httpClient.GetAsync(Uri).Result;

        if (response.IsSuccessStatusCode)
        {
            var responseStream = response.Content.ReadAsStreamAsync().Result;
            WriteToCache(responseStream);
        }
    }));

    return updateCacheTask;
}

我得到了预期的“无法连接到远程服务器”。

我怀疑它与IIS中运行的代码有关,但为什么呢?如何在不需要启动新任务的情况下正确抛出异常?

2 个答案:

答案 0 :(得分:15)

我的直觉告诉我,您正在调用WaitResult进一步调用您的调用堆栈。

如果这是正确的,那么您导致死锁,如I explain on my blog

答案 1 :(得分:-1)

由于我遇到了相同的行为而没有抛出异常,因此我创建了一个示例来演示该问题的可能解决方案:

using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace Exam
{
    public static class Program
    {
        private static async Task<string> GetWebPage(string uri)
        {
            var httpClient = new HttpClient();
            var response = await httpClient.GetAsync(new Uri(uri, UriKind.Absolute), HttpCompletionOption.ResponseContentRead);
            return await response.Content.ReadAsStringAsync();
        }

        public static void Main(string[] args)
        {
            try
            {
                // These two lines do not work - i.e. it terminates the application without any exception being thrown...
                //string s = await GetWebPage(@"https://www.dwd.de/DE/leistungen/klimadatendeutschland/klimadatendeutschland.html");
                //Console.WriteLine(s);

                // This works:
                Task<string> getPageTask = GetWebPage(@"https://www.dwd.de/DE/leistungen/klimadatendeutschland/klimadatendeutschland.html");
                getPageTask.Wait();
                if (getPageTask.IsCompleted)
                    Console.WriteLine(getPageTask.Result);
            }
            catch (AggregateException aex)
            {
                aex.InnerExceptions.AsParallel().ForAll(ex => Console.WriteLine(ex));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.ReadKey();
        }
    }
}

当您另外将 URI 更改为 @"invalid https://...." 之类的内容时,您将检索 AggregateException。 希望,它可以帮助任何人:-)