在 MemoryStream 中下载数据

Downloading data in MemoryStream

我有一个 google table 并下载它:

 Task<byte[]> _res = Http.GetByteArrayAsync(_webPath);

然后我制作一个流:

Stream _stream = new MemoryStream(_res.Result);

并使用此流。 我想解析 table。 在控制台 .net 核心应用程序中,此方法效果很好,但是当我在 blazor 应用程序中使用它时,它不起作用。

Task<byte[]> _res = Http.GetByteArrayAsync(_webPath);
Console.WriteLine("3");
_stream = new MemoryStream(_res.Result);
Console.WriteLine("4");

Result

我也不例外

问题是 _res.Result 阻塞线程直到任务完成,这导致死锁,因为 blazor 只有一个线程。

https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task-1.result?view=netframework-4.7.2

Accessing the property's get accessor blocks the calling thread until the asynchronous operation is complete; it is equivalent to calling the Wait method.

您需要使用 await _res 等待任务,这样线程才不会阻塞并可以完成任务。