简体   繁体   English

在HttpClient上实现异步和等待是不会在OSX上抛出异常

[英]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 . 我用HttpClient使用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. 如果我切换到Windows并尝试运行此应用程序,我将按预期获得TaskCancelledException,但在macOS上它完全正常。

Proof that Google.com was printed into console without exception on macOS 证明Google.com在macOS上毫无例外地打印到控制台中

As I believe the reason behind this behaviour is different implementation of IDisposable in HttpClient on both platforms, but... why? 我相信这种行为背后的原因是两个平台上HttpClient中IDisposable的不同实现,但是......为什么?

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

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

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