2

Currently I am using a two step approach to fetch data from the Web Api and deserialize the CSV records into objects.

var response = await httpClient.GetAsync(queryString);
using (var reader = new StreamReader(await response.Content.ReadAsStreamAsync()))
{
    var csvr = new CsvReader(reader);
    var responseValue = csvr.GetRecords<TrainingDataSetData>().ToList();
    result.Readings.AddRange(responseValue);
}

How do I optimize this code?

2
  • 1
    What are you trying to optimize here? Time? Memory usage? Commented Aug 20, 2018 at 10:49
  • Looks like, the code is reading the response two times. ReadAsStreamAsync and GetRecords. What I really need is var responseValue = csvr.GetRecords<TrainingDataSetData>().ToList(); Only reason I am using the ReadAsStreamAsync is because CSVReader's StreamReader ctor arg. Commented Aug 20, 2018 at 19:15

1 Answer 1

2

If you're trying to avoid creating an intermediate MemoryStream - you could use the GetStreamAsync method on HttpClient, which should return the raw NetworkStream for you pass straight into CsvHelper, instead of ReadAsStreamAsync, which will default to reading the full response into a MemoryStream before returning.

using (var reader = new StreamReader(await httpClient.GetStreamAsync(queryString)))
{
    var csvr = new CsvReader(reader);
    var responseValue = csvr.GetRecords<TrainingDataSetData>().ToList();
    result.Readings.AddRange(responseValue);
}

If you still need access to the HttpResponseMessage, you could achieve the same effect by using HttpCompletionOption.ResponseHeadersRead, which won't buffer the response before returning.

var response = await httpClient.GetAsync(queryString, HttpCompletionOption.ResponseHeadersRead);

As to whether or not this is actually more efficient - this is something that would require benchmarking in your particular environment to decide, since it may be conditional on the size of the response, speed of the network etc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.