http://www.strathweb.com/2013/01/asynchronously-streaming-video-with-asp-net-web-api/
复制&将教程中的代码粘贴到VS 2012项目(使用web api 1?)不产生错误(在添加正确的“using”语句之后).
但是,当我在VS 2013项目中遵循相同的步骤时,我会收到以下两个错误:
Error 1
The call is ambiguous between the following methods or properties:
‘PushStreamContent(System.Func<Stream,HttpContent,TransportContext,Task>
,MediaTypeHeaderValue)’
and
‘PushStreamContent(System.Action<System.IO.Stream,TransportContext>
,MediaTypeHeaderValue)’Error 2
‘void video_stream.Controllers.VideoStream.WriteToStream(System.IO.Stream,System.Net.Http.HttpContent,System.Net.TransportContext)’ has the wrong return type
public async void WriteToStream(Stream outputStream,HttpContent content,TransportContext context) {…}
未标识为< action> web api 1& 2?我真的很困惑,因为我正在针对同一个框架,我似乎无法直观的跨越如何解决它.我改变WriteToStream签名的尝试都失败了.
有没有人知道我需要什么来获取PushStreamContent来接受Web api 2或VS 2013或新的C#中的WriteToStream或者这个代码的区别在哪里生活的线索?
解决方法
response.Content = new PushStreamContent(async (Stream outputStream,TransportContext context) => { try { var buffer = new byte[65536]; using (var video = File.Open(filename,FileMode.Open,FileAccess.Read)) { var length = (int)video.Length; var bytesRead = 1; while (length > 0 && bytesRead > 0) { bytesRead = video.Read(buffer,Math.Min(length,buffer.Length)); await outputStream.WriteAsync(buffer,bytesRead); length -= bytesRead; } } } finally { outputStream.Close(); } });
注意:我做了另一个改变(删除了catch块)到代码来允许异常传播.这样,您的客户知道在服务中发生了一些错误,否则他们会假设一切顺利.