使用System.Threading.Tasks.Task <Stream>而不是Stream

tug*_*erk 7 c# asynchronous wcf-web-api

我在以前版本的WCF Web API上使用了类似下面的方法:

// grab the posted stream
Stream stream = request.Content.ContentReadStream;

// write it to   
using (FileStream fileStream = File.Create(fullFileName, (int)stream.Length)) {

    byte[] bytesInStream = new byte[stream.Length];
    stream.Read(bytesInStream, 0, (int)bytesInStream.Length);
    fileStream.Write(bytesInStream, 0, bytesInStream.Length);
}
Run Code Online (Sandbox Code Playgroud)

但在预览6中,HttpRequestMessage.Content.ContentReadStream财产已经消失.我相信它现在应该像这样:

// grab the posted stream
System.Threading.Tasks.Task<Stream> stream = request.Content.ReadAsStreamAsync();
Run Code Online (Sandbox Code Playgroud)

但我无法弄清楚在using语句中其余代码应该是什么样的.任何人都可以为我提供一种方法吗?

Jon*_*Jon 9

您可能必须根据之前/之后发生的代码进行调整,并且没有错误处理,但是这样的事情:

Task task = request.Content.ReadAsStreamAsync().ContinueWith(t =>
{
    var stream = t.Result;
    using (FileStream fileStream = File.Create(fullFileName, (int) stream.Length)) 
    {
        byte[] bytesInStream = new byte[stream.Length];
        stream.Read(bytesInStream, 0, (int) bytesInStream.Length);
        fileStream.Write(bytesInStream, 0, bytesInStream.Length);
    }
});
Run Code Online (Sandbox Code Playgroud)

如果,在代码的后面,您需要确保已完成此操作,则可以调用task.Wait()并阻止它完成(或抛出异常).

我强烈推荐Stephen Toub的并行编程模式,以加快.NET 4中一些新的异步模式(任务,数据并行等)的速度.


spe*_*der 7

快速而肮脏的修复:

// grab the posted stream
Task<Stream> streamTask = request.Content.ReadAsStreamAsync();
Stream stream = streamTask.Result; //blocks until Task is completed
Run Code Online (Sandbox Code Playgroud)

注意的是,同步版本已从API中删除的事实表明,你真的应该尝试学习新的异步范式,以避免在高负荷下吞噬了许多线程.

你可以举例如:

streamTask.ContinueWith( _ => {
    var stream = streamTask.Result; //result already available, so no blocking
    //work with stream here
} )
Run Code Online (Sandbox Code Playgroud)

或者使用新的异步等待功能:

//async wait until task is complete
var stream = await request.Content.ReadAsStreamAsync(); 
Run Code Online (Sandbox Code Playgroud)

花时间学习异步/等待.它非常方便.