有没有办法阻止 Azure Storage PutBlock 方法超时?

Is there a way of stopping Azure Storage PutBlock method from timing out?

我有一种方法可以将文件上传到 Azure 到我的存储帐户。我的方法很简单

blob = new CloudBlockBlob(new Uri(Uri));
var ms = new MemoryStream(block.Data);
blob.PutBlock(block.BlockId, ms, null)

但有时我的 PutBlock 会抛出异常:

Operation has timed out

StackTrace

at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext)\r\n at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.PutBlock(String blockId, Stream blockData, String contentMD5, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)

内部异常堆栈跟踪

at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)\r\n at System.Net.HttpWebRequest.GetRequestStream()\r\n
at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext)"

这通常发生在我一次异步上传 40 个文件并且文件造成瓶颈时。我现在显然已经将它一次限制为 1 个文件。但我想知道为什么会发生这种情况,是否有一种方法可以防止这种情况发生,这样我就知道这种情况不会再次发生,比如通常较慢的互联网连接?

您可以尝试以下几种方法:

  • 减少并行上传的数量:您可以尝试通过减少并行上传的数量来表示逻辑处理器的数量(或者可能是它的一半)。因此,例如,如果您有 8 核处理器,那么您可以尝试并行上传 4 - 8 个文件。根据我的经验,并行上传更多文件实际上会减慢速度。据我了解,发生大量上下文切换的原因。
  • 减小块大小:您可以尝试减小块大小。
  • 增加请求超时:如果我没记错的话,web请求中默认的请求超时是100秒(https://msdn.microsoft.com/en-us/library/system.net.webrequest.timeout%28v=vs.110%29.aspx). You can try by increasing that. This would take care of timeout from the client side. There's a server side request timeout as well which by default is 30 seconds (with some exceptions). You can read more about server side timeout here: https://msdn.microsoft.com/en-us/library/azure/dd179431.aspx. If I am not mistaken, the default request timeout is 90 seconds. You can try by increasing the timeout. You can specify it in ServerTimeout property of BlobRequestOptions parameter in PutBlock方法。