private static void DoLeaseOperation(CloudBlob blob, string leaseId, LeaseAction action)
        {
            switch (action)
            {
            case LeaseAction.Acquire:
                blob.AcquireLeaseAsync(TimeSpan.FromSeconds(90), leaseId).Wait();
                break;

            case LeaseAction.Renew:
                blob.RenewLeaseAsync(AccessCondition.GenerateLeaseCondition(leaseId)).Wait();
                break;

            case LeaseAction.Release:
                blob.ReleaseLeaseAsync(AccessCondition.GenerateLeaseCondition(leaseId)).Wait();
                break;

            case LeaseAction.Break:
                blob.BreakLeaseAsync(TimeSpan.FromSeconds(90)).Wait();
                break;

            case LeaseAction.Change:
                blob.ChangeLeaseAsync(leaseId, AccessCondition.GenerateLeaseCondition(leaseId)).Wait();
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(action), action, null);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Forcefully deletes a blob.
        /// </summary>
        /// <param name="blob">The CloudBlob to delete.</param>
        /// <returns>A task that completes when the operation is finished.</returns>
        public static async Task <bool> ForceDeleteAsync(CloudBlob blob)
        {
            try
            {
                await blob.DeleteAsync();

                return(true);
            }
            catch (StorageException e) when(BlobDoesNotExist(e))
            {
                return(false);
            }
            catch (StorageException e) when(CannotDeleteBlobWithLease(e))
            {
                try
                {
                    await blob.BreakLeaseAsync(TimeSpan.Zero).ConfigureAwait(false);
                }
                catch
                {
                    // we ignore exceptions in the lease breaking since there could be races
                }

                // retry the delete
                try
                {
                    await blob.DeleteAsync().ConfigureAwait(false);

                    return(true);
                }
                catch (StorageException ex) when(BlobDoesNotExist(ex))
                {
                    return(false);
                }
            }
        }