gpt4 book ai didi

c# - web api 和 web api 2 之间的 PushStreamContent 有什么不同?

转载 作者:可可西里 更新时间:2023-11-01 08:54:42 28 4
gpt4 key购买 nike

我创建了两个相同的 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 项目中执行相同的步骤时,出现以下两个错误:

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

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

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

未被识别为 <action>在 web api 1 和 2 之间?我在这里真的很困惑,因为我的目标是同一个框架,而且我似乎无法直观地了解如何修复它。我更改 WriteToStream 签名的尝试都失败了。

有没有人知道我需要什么才能让 PushStreamContent 接受 Web API 2 或 VS 2013 或新的 C# 中的 WriteToStream 或者这段代码的区别在哪里?

最佳答案

PushStreamContent 构造函数的签名已更改。它的 onStreamAvailable 参数是一个 Action 或 Func 通用类型。问题在于编译器不知道要绑定(bind)到哪种类型。

因此,为了解决错误,将 streamAvailableHandler 转换为 Action:

response.Content = new PushStreamContent((Action<Stream, HttpContent, TransportContext>)streamAvailableHandler);

处理方法是:

private void streamAvailableHandler(Stream stream, HttpContent content, TransportContext context) {
...write to stream
}

关于c# - web api 和 web api 2 之间的 PushStreamContent 有什么不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20031234/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com