gpt4 book ai didi

asp.net - Stream.WriteAsync 抛出远程主机关闭连接异常

转载 作者:行者123 更新时间:2023-12-02 17:57:46 26 4
gpt4 key购买 nike

我有一个 asp.net webforms 应用程序,用于从数据库中检索以 varbinary 格式保存的视频并将其显示为 html5 视频标记。

在谷歌搜索后,我找到了一种使用ASP.Net WebApi异步播放它的方法,它工作得很好

第一个问题

当视频第一次播放并且用户单击播放按钮重播视频时,远程主机关闭了连接。错误代码为 0x800704CD 异常在 await outputStream.WriteAsync(buffer, 0, bytesRead); 行抛出。

第二个问题

当用户单击搜索栏时,视频将从第一个开始播放。

注意

Internet Explorer 11播放视频没有任何问题,但firefox和chrome都有问题。

如何解决这个问题?

这是我的代码:

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

更新

这种方式不能正常工作后,我不得不使用 this way ,但是新方式存在搜索栏问题,当用户点击搜索栏进行搜索计时时,它在 Chrome 和 FireFox 中不起作用。

最佳答案

ASP.NET 不太擅长视频流。第三方视频流解决方案是最佳选择。

有一些视频流服务器(例如 Wowza ),但它们需要安装并且您必须购买许可证。

云流媒体服务是另一种选择。我个人比较喜欢AWS Cloudfront 。他们建议在各个全局分布的内容交付区域中进行分发。它的成本非常便宜,而且您可以确信它能够承受任何流量(即使您的所有用户都会同时观看同一视频)。

关于asp.net - Stream.WriteAsync 抛出远程主机关闭连接异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30255507/

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