当前位置: 首页>>代码示例>>C#>>正文


C# ICloudBlob.AcquireLeaseAsync方法代码示例

本文整理汇总了C#中ICloudBlob.AcquireLeaseAsync方法的典型用法代码示例。如果您正苦于以下问题:C# ICloudBlob.AcquireLeaseAsync方法的具体用法?C# ICloudBlob.AcquireLeaseAsync怎么用?C# ICloudBlob.AcquireLeaseAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ICloudBlob的用法示例。


在下文中一共展示了ICloudBlob.AcquireLeaseAsync方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TryAcquireLeaseAsync

        private async Task<string> TryAcquireLeaseAsync(ICloudBlob blob)
        {
            string leaseId;
            var blobUriString = blob.Uri.ToString();
            try
            {
                var sourceBlobExists = await blob.ExistsAsync();
                if (!sourceBlobExists)
                {
                    return null;
                }

                _jobEventSource.BeginningAcquireLease(blobUriString);
                leaseId = await blob.AcquireLeaseAsync(_defaultLeaseTime, null);
                _jobEventSource.FinishedAcquireLease(blobUriString);
            }
            catch (StorageException storageException)
            {
                // check if this is a 409 Conflict with a StatusDescription stating that "There is already a lease present."
                // or 404 NotFound (might have been removed by another other instance of this job)
                var webException = storageException.InnerException as WebException;
                if (webException != null)
                {
                    var httpWebResponse = webException.Response as HttpWebResponse;
                    if (httpWebResponse != null)
                    {
                        if ((httpWebResponse.StatusCode == HttpStatusCode.Conflict
                            && httpWebResponse.StatusDescription == "There is already a lease present.") || httpWebResponse.StatusCode == HttpStatusCode.NotFound)
                        {
                            _jobEventSource.FailedAcquireLease(blobUriString); // no need to report these in Application Insights
                            return null;
                        }
                    }
                }
                _jobEventSource.FailedAcquireLease(blobUriString);
                ApplicationInsights.TrackException(storageException);

                throw;
            }
            return leaseId;
        }
开发者ID:joyhui,项目名称:NuGet.Jobs,代码行数:41,代码来源:LogFileProvider.cs

示例2: SetLeasedStateAsync

 /// <summary>
 /// Puts the lease on the given blob in a leased state.
 /// </summary>
 /// <param name="blob">The blob with the lease.</param>
 /// <param name="leaseTime">The amount of time on the new lease.</param>
 /// <returns>The lease ID of the current lease.</returns>
 internal static async Task<string> SetLeasedStateAsync(ICloudBlob blob, TimeSpan? leaseTime)
 {
     string leaseId = Guid.NewGuid().ToString();
     await SetAvailableStateAsync(blob);
     return await blob.AcquireLeaseAsync(leaseTime, leaseId);
 }
开发者ID:Juliako,项目名称:azure-sdk-for-net,代码行数:12,代码来源:LeaseTests.cs

示例3: LockInternal

        private async Task<Tuple<IDisposable, string>> LockInternal(ICloudBlob blob)
        {
            string leaseId;

            try
            {
                leaseId = await blob.AcquireLeaseAsync(TimeSpan.FromMinutes(1), null).ConfigureAwait(false);
                LeoTrace.WriteLine("Leased Blob: " + blob.Name);
            }
            catch (StorageException e)
            {
                // If we have a conflict this blob is already locked...
                if (e.RequestInformation.HttpStatusCode == 409)
                {
                    return null;
                }

                if (e.RequestInformation.HttpStatusCode == 404)
                {
                    leaseId = null;
                }
                else
                {
                    throw e.Wrap(blob.Name);
                }
            }

            // May not have had a blob pushed...
            if (leaseId == null)
            {
                try
                {
                    using (var stream = new MemoryStream(new byte[1]))
                    {
                        try
                        {
                            await blob.UploadFromStreamAsync(stream).ConfigureAwait(false);
                        }
                        catch (StorageException) { }  // Just eat storage exceptions at this point... something was created obviously
                    }
                    leaseId = await blob.AcquireLeaseAsync(TimeSpan.FromMinutes(1), null).ConfigureAwait(false);
                    LeoTrace.WriteLine("Created new blob and lease (2 calls): " + blob.Name);
                }
                catch (StorageException e)
                {
                    // If we have a conflict this blob is already locked...
                    if (e.RequestInformation.HttpStatusCode == 409)
                    {
                        return null;
                    }

                    if (e.RequestInformation.HttpStatusCode == 404)
                    {
                        leaseId = null;
                    }
                    else
                    {
                        throw e.Wrap(blob.Name);
                    }
                }
            }

            var condition = AccessCondition.GenerateLeaseCondition(leaseId);

            // Every 30 secs keep the lock renewed
            var keepAlive = AsyncEnumerableEx.CreateTimer(TimeSpan.FromSeconds(30))
                .Select(t =>
                {
                    LeoTrace.WriteLine("Renewed Lease: " + blob.Name);
                    return blob.RenewLeaseAsync(condition);
                })
                .Unwrap()
                .TakeUntilDisposed(null, t =>
                {
                    try
                    {
                        blob.ReleaseLease(condition);
                    }
                    catch (Exception e)
                    {
                        LeoTrace.WriteLine("Release failed: " + e.Message);
                    }
                });


            return Tuple.Create((IDisposable)keepAlive, leaseId);
        }
开发者ID:KalixHealth,项目名称:Kalix.Leo,代码行数:87,代码来源:AzureStore.cs

示例4: SetLeasedStateTask

 /// <summary>
 /// Puts the lease on the given blob in a leased state.
 /// </summary>
 /// <param name="blob">The blob with the lease.</param>
 /// <param name="leaseTime">The amount of time on the new lease.</param>
 /// <returns>The lease ID of the current lease.</returns>
 internal static string SetLeasedStateTask(ICloudBlob blob, TimeSpan? leaseTime)
 {
     string leaseId = Guid.NewGuid().ToString();
     SetAvailableStateTask(blob);
     return blob.AcquireLeaseAsync(leaseTime, leaseId).Result;
 }
开发者ID:huoxudong125,项目名称:azure-sdk-for-net,代码行数:12,代码来源:LeaseTests.cs


注:本文中的ICloudBlob.AcquireLeaseAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。