static async Task<string> AquireAsync(CloudBlockBlob blob, TimeSpan timeStamp) { bool retry = false; do { try { return await blob.AcquireLeaseAsync(timeStamp, null); } catch (StorageException e) { WebException webException = e.InnerException as WebException; if (webException != null) { HttpStatusCode statusCode = ((HttpWebResponse)webException.Response).StatusCode; if (statusCode == HttpStatusCode.Conflict) { Thread.Sleep(500); retry = true; } else if (statusCode == HttpStatusCode.NotFound) { blob.UploadText(string.Empty); retry = true; } } else { throw; } } } while (retry); return null; }
private static async Task AppendToBlob(CloudBlockBlob blockBlob, string content, string md5) { var leaseId = await blockBlob.AcquireLeaseAsync(TimeSpan.FromSeconds(30), null); var access = AccessCondition.GenerateLeaseCondition(leaseId); var options = new BlobRequestOptions(); var context = new OperationContext(); try { var blockId = Convert.ToBase64String(Guid.NewGuid().ToByteArray()); using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(content))) { stream.Position = 0; await blockBlob.PutBlockAsync(blockId, stream, md5, access, options, context); } var blockList = await blockBlob.DownloadBlockListAsync(BlockListingFilter.Committed, access, options, context); var blockIds = blockList.Select(x => x.Name).ToList(); blockIds.Add(blockId); await blockBlob.PutBlockListAsync(blockIds, access, options, context); } catch(Exception ex) { //TODO - logging } finally { await blockBlob.ReleaseLeaseAsync(access); } }
async Task<string> TakeOutSagaLease(CloudBlockBlob dataBlob) { return await dataBlob.AcquireLeaseAsync(null); }