PushStreamContent在web api& web api 2? [英] What is different with PushStreamContent between web api & web api 2?

查看:194
本文介绍了PushStreamContent在web api& web api 2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了两个相同的web api项目,一个在VS 2012中,另一个在VS 2013中,都以4.5 .NET框架为目标。这些项目都是基于Filip W的视频下载教程:
http://www.strathweb.com/2013/01/asynchronously-streaming-video-with-asp-net-web-api/



复制&将教程中的代码粘贴到VS 2012项目(使用web api 1?)不产生错误(在添加适当的using语句之后)。



但是,当我在VS 2013项目中遵循相同的步骤时,我收到以下两个错误:


错误1

调用在以下方法或属性之间是不明确的:
'PushStreamContent(System.Func < Stream,HttpContent,TransportContext,Task> ,MediaTypeHeaderValue)'

'PushStreamContent(System.Action < System.IO.Stream,HttpContent,TransportContext> ,MediaTypeHeaderValue)'



错误2

'void video_stream.Controllers.VideoStream.WriteToStream(System.IO.Stream,System.Net.Http.HttpContent,System.Net.TransportContext)'返回错误键入


所以我的猜测是错误2是真正的问题,因为这个代码:


pub lic async void WriteToStream(Stream outputStream,HttpContent content,TransportContext context){...}


未标识为< action> web api 1& 2?我真的很困惑,因为我正在瞄准同一个框架,我似乎无法直观的跨越如何解决它。有人改变WriteToStream签名的尝试都失败了。



有没有人知道我需要获得PushStreamContent来接受Web api 2或VS 2013中的WriteToStream的线索新的C#或者这段代码的区别在哪里?

解决方案

我不知道这是否是Web API中的一个错误,我们会进行调查。同时您可以尝试以下解决方法:

  response.Content = new PushStreamContent(async(Stream outputStream,HttpContent content,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,0,Math.Min(length,buffer.Length));
await outputStream.WriteAsync缓冲区,0,bytesRead);
length - = bytesRead;
}
}
}
finally
{
outputStream.Close ;
}
});

注意:我做了另一个更改(删除了catch块)到允许异常传播的代码。这样,您的客户知道在服务中发生了一些错误,否则他们会假设一切顺利。


I've created two identical web api projects, one in VS 2012 and another in VS 2013, both targeting the 4.5 .net framework. The projects are based on Filip W's video download tutorial found here: http://www.strathweb.com/2013/01/asynchronously-streaming-video-with-asp-net-web-api/

Copying & pasting the code from the tutorial into the VS 2012 project (using web api 1?) produces no errors (after I add the proper 'using' statements).

However, when I follow the same steps in the VS 2013 project I get the following two errors:

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,HttpContent,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

So my guess is error 2 is the real problem as this code:

public async void WriteToStream(Stream outputStream, HttpContent content, TransportContext context) {...}

Is not identified as an <action> anymore between web api 1 & 2?? I'm really confused here as I'm targeting the same framework, and I can't seem to make the intuitive leap on how to fix it. My attempts at changing the WriteToStream signature have all failed.

Does anybody have a clue on what I need to get PushStreamContent to accept WriteToStream in web api 2 or VS 2013 or the new C# or where ever the difference in this code lives?

解决方案

I am not sure if this is a bug in Web API, we will investigate into it. Meanwhile you can try the following workaround:

response.Content = new PushStreamContent(async (Stream outputStream, HttpContent content, 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, 0, Math.Min(length, buffer.Length));
                await outputStream.WriteAsync(buffer, 0, bytesRead);
                length -= bytesRead;
            }
        }
    }
    finally
    {
        outputStream.Close();
    }
});

Note: I made another change(removed the catch block) to the code to allow exceptions to propagate. This is so that your clients know that some error happened at the service otherwise they would assume everything went smooth.

这篇关于PushStreamContent在web api&amp; web api 2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆