I am writing a controller that returns lines of text from a file, and I want to stream them using IAsyncEnumerable. If I do that, all the strings are returned in the response in a single line of text - this is not particularly friendly to the consumer.
As an example, if I have this:
[HttpGet]
public async IAsyncEnumerable<string> GetAsync() {
for (var i = 0; i < 3; i++) {
yield return i.ToString();
}
}
I get a response that looks like ["0","1","2"]
, but I'd prefer to return something like:
"0"
"1"
"2"
I'm new to AsyncEnumerables in ASP.NET core, but my (possibly wrong) understanding is that if I were to use a TextOutputFormatter I would wind up having to enumerate it in the formatter and that wouldn't be streaming to the client anymore. I tried manually adding Environment.NewLine to the end of the strings, but it just gets encoded in the string, the response doesn't actually contain line breaks.
Is there a good way to do this?