I'm updating some netstandard2.0
code to not read from the HttpRequest.Body
(a Stream
) synchronously (this throws exceptions in netcoreapp3.0
unless you set the AllowSynchronousIO
option which is obviously not a good idea).
I've converted the JSON deserialization part (to use System.Text.Json
), but before it does that it does a sneaky .Peek()
using a StreamReader
(to see if the body is an object or an array) and I'm not exactly sure where to look for a modern alternative that is asynchronous and won't consume the Stream
.
using var reader = new StreamReader(stream);
switch (reader.Peek()) // TODO: Find an async equivalent!
{
case '{':
return await JsonSerializer.DeserializeAsync<GraphQLRequest>(stream);
case '[':
return await JsonSerializer.DeserializeAsync<GraphQLRequest[]>(stream);
default:
// ...
}