private void BeginUploadStream(
BlobTransferContext transferContext,
KeyValuePair<long, int> startAndLength,
MemoryStream memoryStream,
byte[] streamBuffer)
{
if (transferContext.CancellationToken.IsCancellationRequested)
{
return;
}
memoryStream.Seek(0, SeekOrigin.Begin);
OperationContext operationContext = new OperationContext();
operationContext.ClientRequestID = transferContext.ClientRequestId;
Interlocked.Increment(ref transferContext.NumInProgressUploadDownloads);
string blockId =
Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format(CultureInfo.InvariantCulture, "BlockId{0:d7}", (startAndLength.Key / transferContext.BlockSize))));
memoryStream.SetLength(startAndLength.Value);
transferContext.Blob.BeginPutBlock(
blockId,
memoryStream,
null,
AccessCondition.GenerateEmptyCondition(),
transferContext.BlobRequestOptions,
operationContext,
ar =>
{
SuccessfulOrRetryableResult wasWriteSuccessful = EndPutBlock(transferContext, ar);
Interlocked.Decrement(ref transferContext.NumInProgressUploadDownloads);
if (wasWriteSuccessful.IsRetryable)
{
BeginUploadStream(transferContext, startAndLength, memoryStream, streamBuffer);
return;
}
transferContext.MemoryManager.ReleaseBuffer(streamBuffer);
if (!wasWriteSuccessful.IsSuccessful)
{
return;
}
Interlocked.Add(ref transferContext.BytesBlobIOCompleted, startAndLength.Value);
InvokeProgressCallback(transferContext, transferContext.BytesBlobIOCompleted, startAndLength.Value);
if (transferContext.BytesBlobIOCompleted >= transferContext.Length)
{
BeginPutBlockList(transferContext);
}
},
state: null);
}