While reading this question the accepted answer appears to be incorrect as stated by an answer below it and this article that followed. It states that you should wait for ReadAsByteArrayAsync to be complete before continuing with the log due to the fact the Model binder will return null since they are both reading at the same time.
So the accepted solution is:
if (request.Content != null)
{
request.Content.ReadAsByteArrayAsync()
.ContinueWith(task =>
{
var result = Encoding.UTF8.GetString(task.Result);
// Log it somewhere
})
}
But the correct solution should be:
if (request.Content != null)
{
request.Content.ReadAsByteArrayAsync()
.ContinueWith(task =>
{
var result = Encoding.UTF8.GetString(task.Result);
// Log it somewhere
}).Wait(); // Wait until ReadAsByteArrayAsync is completed
}
Now I'm a bit confused as to why the wait really needs to occur, my understanding is continueWith does wait until the task beforehand has completed, then continues. Is there something here I am missing, or don't quite understand about tasks and continues?