I am trying to use HttpClient.PutAsync() to upload several images to Windows Azure storage at the same time. The code snippet looks like:
Task<HttpResponseMessage> putThumbnail = httpclient.PutAsync(new Uri(ThumbnailSas), thumbcontent);
Task<HttpResponseMessage> putImage = httpclient.PutAsync(new Uri(ImageSas), imagecontent);
Task.WaitAll(new Task<HttpResponseMessage>[] {putThumbnail, putImage});
Strangely in this way the server does not return at all so Task.WaitAll will wait forever.
if I change the code using await, the server returns and I can get the result correctly.
HttpResponseMessage result = await httpclient.PutAsync(new Uri(ThumbnailSas), thumbcontent);
How can I batch upload images using HttpClient.PutAsync?
You shouldn't block on async
code, as I describe on my blog .
In this case, you can use Task.WhenAll
:
Task<HttpResponseMessage> putThumbnail = ...;
Task<HttpResponseMessage> putImage = ...;
await Task.WhenAll(putThumbnail, putImage);
Also see Figure 5 in my MSDN article on async
best practices or the end of my async
intro blog post .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.