asp.net - Stream.WriteAsync throws The remote host closed the connection exception -
i have asp.net webforms application , retrieve video database saved in varbinary
format , show html5 video tag.
after googled it, found way should play asynchronously using asp.net webapi
, works fine
first problem
when video played first time , user click on play button replay video, the remote host closed connection. error code 0x800704cd
exception throws @ line await outputstream.writeasync(buffer, 0, bytesread);
.
second problem
when user click on seek bar, video goes played first.
note
internet explorer 11 plays video without problem, firefox , chrome have both problems.
how can solve problem?
here 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 { { 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; } { outputstream.close(); } } }
update
after way didn't worked properly, had use this way, new way have seekbar problem, when user click on seek bar seek time dosn't work in chrome , firefox.
asp.net not @ video streaming. third-party video streaming solution best option.
there few video-streaming servers (like wowza), require installation , have buy license.
cloud streaming service option. prefer aws cloudfront. propose distribution in various globally distributed content delivery zones. costs cheap , can sure survive traffic amount (even if users watch same video simultaneously).
Comments
Post a Comment