Exploring Neon as a Serverless Postgres Alternative for .NET Applications on Azure - Part 1 (Simple ASP.NET Core on App Service)10 lut 2025
Blog | programowanie | .net | c# | azure | IT
Some time ago I've written about streaming JSON objects from server to client in ASP.NET Core. Later I've created a small library with this functionality which supports IAsyncEnumerable<T> and has separated implementations for System.Text.Json and Newtonsoft.Json. The number of downloads on NuGet isn't high but looks like somebody is using it.
Recently, I've received a question, how this JSON objects stream can be consumed with HttpClient. I've once read that instead of writing a response to an email it's better to write a blog post, so here I am.
My demo project consumes the stream from browser client. In order to do it, it's using cool browsers capabilities like Streams API. Unfortunately this doesn't make things obvious enough for somebody not familiar with those APIs to immediately port the implementation.
The way in which one should be thinking about NDJSON response is a string. It's a string that is being append new lines and every line represents a single JSON object. So, in case of .NET, the good old tools we have for "string as stream" are good enough. For example StreamReader. We can use it to asynchronously read lines from response stream and then deserialize them into objects. Below code wraps the whole thing in nice extension method which returns IAsyncEnumerable<T>.
internal static class HttpContentNdjsonExtensions
{
private static readonly JsonSerializerOptions _serializerOptions
= new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNaminoreply@blogger.com (Tomasz Pęczek)