1

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?

4

1 回答 1

2

正如我在博客中所描述的那样,您不应该阻止async代码。

在这种情况下,您可以使用Task.WhenAll

Task<HttpResponseMessage> putThumbnail = ...;
Task<HttpResponseMessage> putImage = ...;
await Task.WhenAll(putThumbnail, putImage);

另请参阅我关于最佳实践的MSDN 文章中的async图 5或我的async介绍性博客文章的结尾。

于 2013-09-27T17:31:53.520 回答