Skip to content

Instantly share code, notes, and snippets.

@sergioprates
Created November 18, 2018 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergioprates/1f9e2e739713690e3802d90d2b4a40d3 to your computer and use it in GitHub Desktop.
Save sergioprates/1f9e2e739713690e3802d90d2b4a40d3 to your computer and use it in GitHub Desktop.
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace DotNetCoreStreaming.Results
{
public class PushStreamResult : IActionResult
{
private readonly Action<Stream, CancellationToken> _onStreamAvailable;
private readonly string _contentType;
private readonly CancellationToken _requestAborted;
public PushStreamResult(Action<Stream, CancellationToken> onStreamAvailable, string contentType, CancellationToken requestAborted)
{
_onStreamAvailable = onStreamAvailable;
_contentType = contentType;
_requestAborted = requestAborted;
}
public Task ExecuteResultAsync(ActionContext context)
{
var stream = context.HttpContext.Response.Body;
context.HttpContext.Response.GetTypedHeaders().ContentType = new MediaTypeHeaderValue(_contentType);
_onStreamAvailable(stream, _requestAborted);
return Task.CompletedTask;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment