Пример #1
0
        /// <summary>
        /// Creates a cloud lock, returning a class representing the lock.
        /// </summary>
        /// <param name="blob">The blob to lock.</param>
        /// <returns>A <see cref="CloudLock"/> representing the lock, or null if no lock could be obtained.</returns>
        public static async Task<CloudLock> LockAsync(CloudBlob blob)
        {
            // this creates the blob container
            await blob.Container.CreateIfNotExistAsync();

            // this creates the blob
            await blob.CreateIfNotExistAsync();

            // this leases the (possibly newly created) blob
            var leaseId = await CloudBlobExtensions.TryAcquireLease(blob);

            if (leaseId == null)
                return null;

            // this maintains the lease
            var cts = new CancellationTokenSource();
            return new CloudLock(blob, leaseId, cts);
        }