I have the following piece of code to read and process the response of a Http get request using StreamReader:
try
{
Stream stream = await ExecuteRequestAsync(uriBuilder.Uri, HttpMethod.Get).ConfigureAwait(false);
if (stream != null)
{
using (StreamReader sr = new StreamReader(stream))
{
using (JsonTextReader reader = new JsonTextReader(sr))
{
...
}
}
}
}
catch (Exception ReadingStreamException)
{
}
private async Task<stream> ExecuteRequestAsync(Uri uri, HttpMethod method)
{
Stream stream;
using (HttpRequestMessage httpRequestMessage = new HttpRequestMessage(method, uri))
{
try
{
stopwatch.Start();
using (HttpResponseMessage responseMessage = await this.httpClient.SendAsync(httpRequestMessage).ConfigureAwait(false))
{
stream = await responseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false);
}
}
catch(Exception GettingStreamException)
{
// Error checking code
}
return stream;
}
The line using (StreamReader sr = new StreamReader(stream)), is throwing an exception to ReadingStreamException of type ArgumentException with the detail "Stream was not readable". Is there something wrong with the above code?
This happens because when you dispose
HttpResponseMessage, it disposes the underlying stream. From the source:You can either copy the stream, or simply not dispose
HttpResponseMessage, as you'll dispose the underlying stream anyway when passing it toStreamReader. I'd go with the latter: