/// <summary>
 /// Puts the lease on the given blob in a broken state due to the break period expiring.
 /// </summary>
 /// <param name="blob">The blob with the lease.</param>
 /// <returns>The lease ID of the broken lease.</returns>
 internal static async Task<string> SetTimeBrokenStateAsync(CloudBlob blob)
 {
     string leaseId = await SetLeasedStateAsync(blob, null /* infinite lease */);
     await blob.BreakLeaseAsync(TimeSpan.FromSeconds(1));
     await Task.Delay(TimeSpan.FromSeconds(2));
     return leaseId;
 }
 /// <summary>
 /// Puts the lease on the given blob in a broken state due to a break period of zero.
 /// </summary>
 /// <param name="blob">The blob with the lease.</param>
 /// <returns>The lease ID of the broken lease.</returns>
 internal static async Task<string> SetInstantBrokenStateAsync(CloudBlob blob)
 {
     string leaseId = await SetLeasedStateAsync(blob, null /* infinite lease */);
     await blob.BreakLeaseAsync(TimeSpan.Zero);
     return leaseId;
 }
 /// <summary>
 /// Puts the lease on the given blob in a breaking state for 60 seconds.
 /// </summary>
 /// <param name="blob">The blob with the lease.</param>
 /// <returns>The lease ID of the current (but breaking) lease.</returns>
 internal static async Task<string> SetBreakingStateAsync(CloudBlob blob)
 {
     string leaseId = await SetLeasedStateAsync(blob, null /* infinite lease */);
     await blob.BreakLeaseAsync(TimeSpan.FromSeconds(60));
     return leaseId;
 }
        /// <summary>
        /// Puts the lease on the given blob in an available state.
        /// </summary>
        /// <param name="blob">The blob with the lease.</param>
        internal static async Task SetAvailableStateAsync(CloudBlob blob)
        {
            bool shouldBreakFirst = false;

            OperationContext operationContext = new OperationContext();
            try
            {
                await blob.DeleteIfExistsAsync(DeleteSnapshotsOption.None, null, null, operationContext);
            }
            catch (Exception)
            {
                if (operationContext.LastResult.ExtendedErrorInformation.ErrorCode == BlobErrorCodeStrings.LeaseIdMissing)
                {
                    shouldBreakFirst = true;
                }
                else
                {
                    throw;
                }
            }

            if (shouldBreakFirst)
            {
                await blob.BreakLeaseAsync(TimeSpan.Zero);
                await blob.DeleteAsync();
            }
            await CreateBlobAsync(blob);
        }
 /// <summary>
 /// Puts the lease on the given blob in a broken state due to a break period of zero.
 /// </summary>
 /// <param name="blob">The blob with the lease.</param>
 /// <returns>The lease ID of the broken lease.</returns>
 internal static string SetInstantBrokenStateTask(CloudBlob blob)
 {
     string leaseId = SetLeasedStateTask(blob, null /* infinite lease */);
     blob.BreakLeaseAsync(TimeSpan.Zero).Wait();            
     return leaseId;
 }
        /// <summary>
        /// Puts the lease on the given blob in a breaking state for 60 seconds.
        /// </summary>
        /// <param name="blob">The blob with the lease.</param>
        /// <returns>The lease ID of the current (but breaking) lease.</returns>
        internal static string SetBreakingStateTask(CloudBlob blob)
        {
            string leaseId = SetLeasedStateTask(blob, null /* infinite lease */);

            blob.BreakLeaseAsync(TimeSpan.FromSeconds(60)).Wait();
            
            return leaseId;
        }
        /// <summary>
        /// Puts the lease on the given blob in an available state.
        /// </summary>
        /// <param name="blob">The blob with the lease.</param>
        internal static void SetAvailableStateTask(CloudBlob blob)
        {
            bool shouldBreakFirst = false;

            try
            {
                blob.DeleteIfExistsAsync().Wait();
            }
            catch (AggregateException ex)
            {
                StorageException exception = ex.InnerException as StorageException;
                if (exception == null)
                {
                    throw;
                }
                
                if (exception.RequestInformation.ExtendedErrorInformation.ErrorCode == BlobErrorCodeStrings.LeaseIdMissing)
                {
                    shouldBreakFirst = true;
                }
                else
                {
                    throw;
                }
            }

            if (shouldBreakFirst)
            {
                blob.BreakLeaseAsync(TimeSpan.Zero).Wait();

                blob.DeleteAsync().Wait();
            }

            CreateBlobTask(blob);
        }