【发布时间】:2018-09-19 08:50:12
【问题描述】:
在阅读了关于 eliding async and await 的 Stephen Cleary 博客文章后,我决定去尝试一下。我使用 Visual Studio For Mac 使用 HttpClient 编写了非常简单的控制台应用程序。
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");
}
根据博客文章,它应该抛出异常但它没有。如果我切换到 Windows 并尝试运行此应用程序,我将按预期得到 TaskCancelledException,但在 macOS 上它工作得非常好。
Proof that Google.com was printed into console without exception on macOS
我相信这种行为背后的原因是两个平台上 HttpClient 中 IDisposable 的不同实现,但是......为什么?
【问题讨论】:
-
您的目标是什么 .NET 版本?是 .net 核心吗?
-
.NET Framework 4.7.1 和最新的 C#。 HttpClient 版本 2.2.29。在 macOS 上构建的执行文件 (.exe) 在 Windows 上失败,但在 macOS 上失败。
-
@DominikPrzywara 你确定它的 .NET 4.7.1 吗?据我所知,您只能在 Mac 上构建 .NET Core
-
@maccettura 是的,你可以根据这个docs.microsoft.com/en-us/dotnet/core/macos-prerequisites 和我在我的项目中看到的内容:D(使用 macOS 创建:P)
-
@DominikPrzywara 我说错了。我的意思是,据我所知,您只能在 Mac 上构建 .NET Core
标签: c# .net macos visual-studio async-await