在Framework 4.0中流ReadAsync / WriteAsync

时间:2015-08-26 09:34:02

标签: c#-4.0 async-await vimeo-api

private async Task<byte[]> ReadDataStream(long totalLength)
{
    var buffer = new byte[BUFFER_SIZE];
    int read = 0;
    int totalRead = 0;
    using (var ms = new MemoryStream())
    {
        while (totalRead < totalLength)
        {
            read = await Data.ReadAsync(buffer, 0, buffer.Length);
            totalRead += read;
            await ms.WriteAsync(buffer, 0, read);
        }
        return ms.ToArray();
    }
}

尝试使用ReadAsync读取流数据,但它说在框架4.0中不支持,是否有其他方法可以读取流异步

错误:

Error   2   'System.IO.Stream' does not contain a definition for 'ReadAsync' and no extension method 'ReadAsync' accepting a first argument of type 'System.IO.Stream' could be found (are you missing a using directive or an assembly reference?) C:\vimeo-dot-net\src\VimeoDotNet\Net\BinaryContent.cs   130 39  VimeoDotNet
Error   3   'System.IO.MemoryStream' does not contain a definition for 'WriteAsync' and no extension method 'WriteAsync' accepting a first argument of type 'System.IO.MemoryStream' could be found (are you missing a using directive or an assembly reference?)   C:\vimeo-dot-net\src\VimeoDotNet\Net\BinaryContent.cs   132 30  VimeoDotNet

1 个答案:

答案 0 :(得分:2)

使用Yuval的建议有效。

在包管理器控制台中,键入以下内容:

  

Install-Package Microsoft.Bcl.Async