Skip to content

Instantly share code, notes, and snippets.

@NickDarvey
Last active October 14, 2020 06:27
Show Gist options
  • Save NickDarvey/c368864eda1ee108e2c1373bc85778e7 to your computer and use it in GitHub Desktop.
Save NickDarvey/c368864eda1ee108e2c1373bc85778e7 to your computer and use it in GitHub Desktop.
DelegateResult, an IActionResult useful for Azure Functions
open Microsoft.AspNetCore.Mvc
type DelegateResult(write : Stream -> System.Threading.Tasks.Task, encoding : System.Net.Http.Headers.MediaTypeHeaderValue, status : int) =
interface IActionResult with
member this.ExecuteResultAsync(ctx) =
ctx.HttpContext.Response.StatusCode <- status
ctx.HttpContext.Response.ContentType <- encoding.ToString ()
write ctx.HttpContext.Response.Body
open System.Text.Json
let private options = JsonSerializerOptions()
let private encoding = System.Net.Http.Headers.MediaTypeHeaderValue "application/json"
encoding.CharSet <- System.Text.Encoding.UTF8.WebName
let serialize value = (fun stream -> JsonSerializer.SerializeAsync(stream, value, options)), encoding
let write, encoding = serialize {| thing = 42 |}
DelegateResult (write, encoding, 200) :> IActionResult
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment