简体   繁体   English

使用HttpClient.PutAsync()批量上传

[英]Batch upload using HttpClient.PutAsync()

I am trying to use HttpClient.PutAsync() to upload several images to Windows Azure storage at the same time. 我正在尝试使用HttpClient.PutAsync()同时将多个图像上传到Windows Azure存储。 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. 奇怪的是,服务器根本不返回,因此Task.WaitAll将永远等待。

if I change the code using await, the server returns and I can get the result correctly. 如果我使用await更改代码,服务器将返回并且我可以正确获得结果。

HttpResponseMessage result = await httpclient.PutAsync(new Uri(ThumbnailSas), thumbcontent);

How can I batch upload images using HttpClient.PutAsync? 如何使用HttpClient.PutAsync批量上传图像?

You shouldn't block on async code, as I describe on my blog . 如我在博客中所述 ,您不应阻止async代码。

In this case, you can use Task.WhenAll : 在这种情况下,您可以使用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 . 另请参阅我的MSDN文章中有关async最佳实践的图5或async介绍博客文章的结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM