Converting video streaming site from Web API to DotNet Core
<p>I've got a site that streams large tutorial videos to an intranet, and I'm trying to convert it to DotNet Core.  The existing solution is based on the <a href="http://www.strathweb.com/2013/01/asynchronously-streaming-video-with-asp-net-web-api/" rel="nofollow">StrathWeb.com code</a> that creates a video stream object that reads a video file in chunks and pushes it down to a Request.CreateResponse() via a PushStreamContent call.</p>

<p>I've converted it to DotNet Core using the Microsoft.AspNetCore.Mvc.WebApiCompatShiv but it no longer plays the video; the player now just says "Error: Unsupported video type or invalid file path" and I think Fiddler is showing that it's just returning a Json file and no actual stream.  </p>

<p>I have also tried it with a FileStreamResult and it works fine, but it does seem to read the entire file in which is a problem with bigger videos.</p>

<p>My quick question is: what have I done wrong in my conversion?</p>

<p>The bigger question is: what is the correct way to stream large video files in DotNet Core without using the compatibility shim?</p>

<pre><code>public class VideoController : ApiController
{
    public HttpResponseMessage VidStream()
    {
        var video = new VideoStream(@"c:\testhome\testvideo", "mp4");

        var response = Request.CreateResponse(HttpStatusCode.PartialContent);

        var mediaType = new MediaTypeHeaderValue("video/" + ext);
        var action = new Action&lt;Stream, HttpContent, TransportContext&gt;(video.WriteToStream);

        response.Content = new PushStreamContent(action, mediaType.ToString());

        return response;
    }
}

/* VideoStream.cs */
public class VideoStream
{
    private readonly string _filename;

    public VideoStream(string filename, string ext)
    {
        _filename = filename + "." + ext;
    }

    public async void 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 &gt; 0 &amp;&amp; bytesRead &gt; 0)
                {
                    bytesRead = video.Read(buffer, 0, Math.Min(length, buffer.Length));
                    await outputStream.WriteAsync(buffer, 0, bytesRead);
                    length -= bytesRead;
                }
            }
        }
        catch (Exception ex)    // HttpException
        {
            System.Diagnostics.Debug.WriteLine(ex.Message);
            return;
        }
        finally
        {
            outputStream.Dispose();
        }
    }
}

/* Startup.cs */
public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddTransient&lt;IContentNegotiator, DefaultContentNegotiator&gt;();
    ...
}
</code></pre>