How can I properly extract an HttpResponseMessage
Content
property (of type HttpContent
) in .NET Standard 2.0 plain synchronous manner, acquired after using asynchronous await HttpClient.SendAsync(request)
, without using the deadlock dangerous task Result
property nor Wait()
method?
Background: A .NET Standard 2.0 C# library consumes some API in asynchronous manner using await HttpClient.SendAsync()
, but sometimes it needs to extract and de-serialize the response content on demand in plain synchronous manner, i.e. via a property that initializes some Lazy object for it.
The API request is like this:
HttpResponseMessage response = await HttpClient.SendAsync(request).ConfigureAwait(false);
while the corresponding asynchronous Content
extraction to string operation would be like this:
string content = await response.Content.ReadAsStringAsync();
Then, the lazy property (synchronous) initializer would acquire the string result like this:
string result = (await response.Content.ReadAsStringAsync()).Result;
or, sometimes better:
string result = Task.Run( () => response.Content.ReadAsStringAsync() ).Result;
Both cases, however, might cause deadlock, depending on the context.
Challenge: The suggested solution for libraries is to use plain synchronous reading in synchronous calls, i.e. using a synchronous response.Content.ReadAsString()
instead of asynchronous response.Content.ReadAsStringAsync()
.
However, such synchronous method does not exist and is not described by the .NET Standard 2.0 HttpContent
class documentation, although it does in other version(s), i.e. the .NET 6 version of the class.
Is it possible to implement it in some way in .NET Standard 2.0, i.e. using synchronous StreamReader
and JsonSerializer
, or any other safe way and how?
Thanks in advance.