Created
November 18, 2018 11:22
-
-
Save sergioprates/1f9e2e739713690e3802d90d2b4a40d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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