Stream.WriteAsync抛出远程主机关闭了连接异常 [英] Stream.WriteAsync throws The remote host closed the connection exception
问题描述
我有一个asp.net web表单应用程序,并检索从保存在 VARBINARY
格式的数据库的视频并显示为HTML5视频标签。
I have an asp.net webforms application and to retrieve video from database that saved in varbinary
format and show it as html5 video tag.
经过GOOGLE了它,我发现了一个办法,我应该发挥它使用异步 ASP.Net的WebAPI
,它工作正常。
after a googled it, i found a way that i should play it asynchronously using ASP.Net WebApi
, it works fine
第一个问题
当视频首先播放时间和播放按钮,用户点击播放视频,远程主机关闭了连接。错误code是0x800704CD
例外抛出在行等待outputStream.WriteAsync(缓冲,0,读取动作);
When video played first time and the user click on play button to replay the video, The remote host closed the connection. The error code is 0x800704CD
exception throws at line await outputStream.WriteAsync(buffer, 0, bytesRead);
.
第二个问题
在上寻求栏用户点击,视频云,从第一次打。
When user click on seek bar, the video goes to played from first.
注意
的Internet Explorer 11 播放没有任何问题的视频,但Firefox和Chrome有两个问题。
Internet Explorer 11 plays the video without any problem, but firefox and chrome have both problems.
我怎样才能解决这个问题?
how can i solve this problem?
下面是我的codeS:
Here is my codes:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.EnableCors();
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "VideoApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
public class VideoController : ApiController
{
public IVideoRepository videoRepository;
public HttpResponseMessage Get(long id)
{
try
{
videoRepository = new VideoRepository();
Video video = videoRepository.load(id);
if (video != null)
{
var videoStream = new VideoStream(video.fileContent);
string ext = video.extension;
var response = Request.CreateResponse();
response.Content = new PushStreamContent((Action<Stream, HttpContent, TransportContext>)videoStream.WriteToStream, new MediaTypeHeaderValue("video/" + ext));
response.Content.Headers.Add("Content-Disposition", "attachment;filename=" + video.fullName.Replace(" ", ""));
response.Content.Headers.Add("Content-Length", videoStream.FileLength.ToString());
return response;
}
else
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
}
catch (Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.ServiceUnavailable, e);
}
}
}
public class VideoStream
{
private readonly byte[] _fileContent;
private long _contentLength;
public long FileLength
{
get { return _contentLength; }
}
public VideoStream(byte[] content)
{
_contentLength = content.Length;
_fileContent = content;
}
public async void WriteToStream(Stream outputStream, HttpContent content, TransportContext context)
{
try
{
var buffer = new byte[65536];
MemoryStream memoryStream = new MemoryStream();
memoryStream.Write(_fileContent, 0, _fileContent.Length);
memoryStream.Position = 0;
using (memoryStream)
{
var length = (int)memoryStream.Length;
var bytesRead = 1;
while (length > 0 && bytesRead > 0)
{
bytesRead = memoryStream.Read(buffer, 0, Math.Min(length, buffer.Length));
await outputStream.WriteAsync(buffer, 0, bytesRead);
length -= bytesRead;
}
}
}
catch (Exception e)
{
throw e;
}
finally
{
outputStream.Close();
}
}
}
更新
在这样没有正确的工作,我不得不使用<一个href=\"http://stackoverflow.com/questions/30379829/html5-video-seekbar-doesnt-work-in-chrome-and-firefox-in-asp-net-webforms\">this这样,但新的方式有搜索栏的问题,在搜索栏,当用户点击寻求一次在Chrome和FireFox不到风度的工作。
after this way didn't worked properly, i had to use this way, but the new way have seekbar problem, when user click on seek bar to seek to time it dosn't work in Chrome and FireFox.
推荐答案
ASP.NET是不是视频流非常好。第三方视频流解决方案是最好的选择。
ASP.NET is not very good at video streaming. Third-party video streaming solution is the best option.
有几个视频流媒体服务器(如 Wowza ),但他们需要安装,你必须购买许可证。
There are a few video-streaming servers (like Wowza), but they require installation and you have to buy license.
云流服务是另一种选择。我个人preFER AWS的Cloudfront 。他们提出的各种全球分布式内容交付区域分布。它的价格真的很便宜,你可以肯定,这将生存任何交通量(即使所有用户将同时收看相同的视频)。
Cloud streaming service is another option. I personally prefer AWS Cloudfront. They propose distribution in various globally distributed content delivery zones. It costs really cheap and you can be sure that it will survive any traffic amount (even if all your users will watch the same video simultaneously).
这篇关于Stream.WriteAsync抛出远程主机关闭了连接异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!