Last active
October 14, 2020 06:27
-
-
Save NickDarvey/c368864eda1ee108e2c1373bc85778e7 to your computer and use it in GitHub Desktop.
DelegateResult, an IActionResult useful for Azure Functions
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
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