I'm developing a WPF web client using dotnet Core 3.x, and I'm utilising the System.Text.Json APIs. I am trying to use a Stream
to pass the data between objects to minimise peak memory usage, as some large messages are being sent.
The wrapper method I've written thus far looks like the following:
public async Task<TResponse> PutItem<TItem, TResponse>(string path, TItem item)
{
HttpResponseMessage response;
await using (Stream stream = new MemoryStream())
{
await JsonSerializer.SerializeAsync(stream, item);
var requestContent = new StreamContent(stream);
requestContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
response = await _client.PutAsync(path, requestContent);
}
if (!response.IsSuccessStatusCode || response.Content == null)
{
return default;
}
string content = await response.Content.ReadAsStringAsync();
TResponse decodedResponse = JsonSerializer.Deserialize<TResponse>(content);
return decodedResponse;
}
However it does not appear to be writing any content when PUTting to the server.
I have seen users of earlier APIs utilise the PushStreamContent
class, however this doesn't appear to exist in dotnet Core.
Any ideas would be very much appreciated, thanks!