简体   繁体   中英

Eliding async and await on HttpClient is not throwing exception on OSX

After reading Stephen Cleary blog post about eliding async and await I've decided to go and play around with it. I wrote very simple console app with HttpClient using Visual Studio For Mac .

public static async Task Main(string[] args)
{
    Console.WriteLine(await Get());
    Console.WriteLine("Hello World!");
}

public static Task<string> Get()
{
    using (var http = new HttpClient())
        return http.GetStringAsync("http://google.com");
}

According to blog post it should throw an exception but it didn't . If I switch to Windows and try to run this app, I will get TaskCancelledException as expected, but on macOS it works perfectly fine.

Proof that Google.com was printed into console without exception on macOS

As I believe the reason behind this behaviour is different implementation of IDisposable in HttpClient on both platforms, but... why?

比较单声道存储库dotnet存储库我发现,HttpClient的单声道实现不会在Dispose方法中调用CancellationTokenSource.Cancel(),我相信这是平台之间不一致的原因。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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