I see some really odd behavior using HttpClient. I wrote an application just using the HttpClient class with tons of logging. Based on some dotPeek, would seem I am basically doing what the API does.
What it logging shows is that the
Stream stream = await HttpClient:GetStreamAsync()
and passing the https://..../sharing/rest/content/ itemId/data url returns before the stream is completely downloaded. This method returned in about 700 ms. However, as I copy the stream to a file it times out at this mystery 100 seconds mark with the same error the API throws (this only occurs on remote slow connections, internal connections all go fast enough)
System.IO.IOException: The read operation failed, see inner exception. ---> System.Net.WebException: The request was aborted: The request was canceled.
at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Cache.ForwardingReadStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Http.HttpClientHandler.WebExceptionWrapperStream.Read(Byte[] buffer, Int32 offset, Int32 count)
--- End of inner exception stack trace ---
at System.Net.Http.HttpClientHandler.WebExceptionWrapperStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Http.DelegatingStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.Stream.InternalCopyTo(Stream destination, Int32 bufferSize)
at System.IO.Stream.CopyTo(Stream destination)
at HttpDownloader.Downloader.<DownloadData>d__2.MoveNext()
The only thing that makes sense based on what I see is that the stream has really not downloaded completely when HttpClient:GetStreamAsync() moves on to the next line, but is actually continuing to stream data across the wire as the file is being written. This is contrary to my understanding of how the HttpClient async methods are supposed to work. My understanding was that the method does not return back to the caller method until the end of the stream being downloaded.
Just for yucks, I went old school and used WebClient to do a synchronous download. In this case everything worked fine. One files took over 2 minutes to download was successful.
My conclusion, I have absolutely no idea. The logging I see does not make sense with my understanding of how async/await is supposed to work. What happens at this magical 100 seconds, I have no clue. Since the download actually occurs at application start up (downloading offline replicas), we may just go with the synchronous download and let the UI be frozen.
Thanks,
-Joe