ASP.Net Core Web Api中的异步视频流不起作用
发布时间:2020-12-16 04:32:06 所属栏目:asp.Net 来源:网络整理
导读:我之前使用过 http://www.strathweb.com/2013/01/asynchronously-streaming-video-with-asp-net-web-api/这种技术,并且非常适合异步视频流. 但对于ASP.NET Core,这种方式并没有按预期工作. 通过视频流类是: public class VideoStream{ private readonly str
我之前使用过
http://www.strathweb.com/2013/01/asynchronously-streaming-video-with-asp-net-web-api/这种技术,并且非常适合异步视频流.
但对于ASP.NET Core,这种方式并没有按预期工作. 通过视频流类是: public class VideoStream { private readonly string _filename; public VideoStream(string filename) { _filename = filename; } public async Task WriteToStream(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,Math.Min(length,buffer.Length)); await outputStream.WriteAsync(buffer,bytesRead); length -= bytesRead; } } } catch (Exception) { return; } finally { outputStream.Flush(); outputStream.Dispose(); } } } 我对视频流请求有以下操作: [HttpGet] [Route("[action]")] public IActionResult GetVideo(int id) { var fileName = GetVideoFileName(id); var video = new VideoStream(fileName); var response = new HttpResponseMessage { Content = new PushStreamContent(video.WriteToStream,new MediaTypeHeaderValue("video/mp4")) }; var objectResult = new ObjectResult(response); objectResult.ContentTypes.Add(new Microsoft.Net.Http.Headers.MediaTypeHeaderValue("video/mp4")); return objectResult; } 由于默认情况下Asp.Net Core没有内置的Media Formatter for video / mp4我创建了以下自定义媒体格式化程序 public class VideoOutputFormatter : IOutputFormatter { public bool CanWriteResult(OutputFormatterCanWriteContext context) { if (context == null) throw new ArgumentNullException(nameof(context)); return true; } public async Task WriteAsync(OutputFormatterWriteContext context) { if (context == null) throw new ArgumentNullException(nameof(context)); var response = context.HttpContext.Response; response.ContentType = "video/mp4"; How to impelemnt ??? } } 并将以下行添加到Startup.cs services.AddMvc(options => { options.OutputFormatters.Add(new VideoOutputFormatter()); }); 它实际上调用我的自定义格式化程序 解决方法
查看Asp.NET Core的源代码真的帮助我找到了这个的答案.他们有一个
StreamOutputFormatter级别,非常接近您想要使用的级别.我只需要修改它来寻找PushStreamContent,它就像一个魅力.
这是我完整的VideoOutputFormatter: public class VideoOutputFormatter : IOutputFormatter { public bool CanWriteResult(OutputFormatterCanWriteContext context) { if (context == null) throw new ArgumentNullException(nameof(context)); if (context.Object is PushStreamContent) return true; return false; } public async Task WriteAsync(OutputFormatterWriteContext context) { if (context == null) throw new ArgumentNullException(nameof(context)); using (var stream = ((PushStreamContent)context.Object)) { var response = context.HttpContext.Response; if (context.ContentType != null) { response.ContentType = context.ContentType.ToString(); } await stream.CopyToAsync(response.Body); } } } 您不希望将HttpResponseMessage包装在控制器中的ObjectResult中,而是将PushStreamContent对象推送到ObjectResult中.您仍然需要在ObjectResult上设置MediaTypeHeaderValue. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 由for V.S. for each想到的
- ASP.NET MVC4 设置路由的命名空间 namespace
- asp.net – 连接到源文件XYZ的撤消管理器时出错
- asp.net – 如何提供不会被Hotmail删除的跟踪图像?
- asp-classic – 如何在经典ASP中遍历集合?
- asp.net-mvc-3 – ASP.NET MVC 3书籍计划
- asp.net-mvc – ASP.NET MVC 4动作过滤器问题
- asp.net – 什么是建议替代常见的破坏app_offline.htm黑客?
- asp.net-mvc – 在MVC ASP.NET中使用/显示RSS源的简单方法
- ASP.NET MVC 3:主模板的ViewModel?
推荐文章
站长推荐
- asp.net-mvc – ASP MVC:为什么我的视图中看不到
- asp.net – EF更新记录属性后在SaveChanges()上抛
- ASP脚本中的Python 500服务器错误
- asp.net-mvc – ViewModel有自己的逻辑吗?
- asp.net-mvc – 如何在ASP.NET MVC Web API中将U
- asp.net-mvc – Web场中的nHibernate策略
- 定时器_在.net core3.0 webapi中添加自定义定时器
- asp.net – 如何在站点地图的单独窗口中打开文档
- asp.net – 用于错误处理和丢失图像的HttpModule
- asp.net-mvc – 创建一个texarea帮助器,它将视图
热点阅读