什么是网页API和放大器之间PushStreamContent不同。网页API 2? [英] What is different with PushStreamContent between web api & web api 2?

查看:645
本文介绍了什么是网页API和放大器之间PushStreamContent不同。网页API 2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了两个相同的网页API项目,一个在VS 2012年陆续在VS 2013年,这两个目标的4.5 .NET框架。该项目是基于菲利普W公司的视频下载教程在这里找到:
http://www.strathweb.com/2013/01/asynchronously-streaming-video-with-asp-net-web-api/

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/

&复制放大器;从教程到VS 2012项目粘贴code(使用Web API 1?)不产生错误(在我添加适当的'使用'语句)。

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).

然而,当我跟随在VS 2013项目相同的步骤,我得到以下两个错误:

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

错误1结果
  电话是以下方法或属性之间暧昧:
  PushStreamContent(System.Func <流,HttpContent,TransportContext,任务> ,MediaTypeHeaderValue)
  和
  PushStreamContent(System.Action <的System.IO.Stream,HttpContent,TransportContext> ,MediaTypeHeaderValue)

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)'

错误2结果
  无效video_stream.Controllers.VideoStream.WriteToStream(的System.IO.Stream,System.Net.Http.HttpContent,System.Net.TransportContext)'有错误的返回类型

Error 2
'void video_stream.Controllers.VideoStream.WriteToStream(System.IO.Stream, System.Net.Http.HttpContent, System.Net.TransportContext)' has the wrong return type

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

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

公共异步无效WriteToStream(流的OutputStream,HttpContent内容,TransportContext上下文){...}

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

没有被识别为&lt;作用&GT; 了网页API 1安培之间; 2?我真的在这里感到困惑,我瞄准同一个框架,而我似乎无法就如何解决它直观的飞跃。我在改变WriteToStream签名的尝试都失败了。

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.

没有任何人有什么我需要获得PushStreamContent接受网页API 2或VS 2013或新的C#或者其它任何WriteToStream线索在这一code生活有什么区别?

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?

推荐答案

我不知道这是Web API的一个bug,我们会调查到它。同时,你可以尝试以下解决方法:

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();
    }
});

注意:我做了另一个变化(除去catch块)到code到允许例外传播。这是为了让你的客户知道在服务发生了一些错误否则将承担一切都很顺利。

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.

这篇关于什么是网页API和放大器之间PushStreamContent不同。网页API 2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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