Helper library to maintain a lease while in a using block. Attempts to autorenew a 90 second lease every 40 seconds (customisable) rather than indefinitely, meaning the lease isn't locked forever if the instance crashes. Based on https://github.com/smarx/WazStorageExtensions pending a pull request we have sent to this project.
Inheritance: IDisposable
 public void Allow_lease_operations_when_lease_id_provided()
 {
     using (var lease = new AutoRenewLease(new ConsoleFactory(), LoggerLevel.Debug, _blob))
     {
         _blob.SetMetadata(lease.LeaseId);
     }
 }
        public void Start()
        {
            _cancellationToken = new CancellationTokenSource();
            _resetEvent = new ManualResetEvent(false);

            Task.Factory.StartNew(() =>
            {
                _logger.Debug("Starting web deploy leasing thread...");

                while (true)
                {
                    try
                    {
                        var blob = AzureRoleEnvironment.WebDeployLeaseBlob();
                        using (var lease = new AutoRenewLease(_loggerFactory, _logLevel, blob))
                        {
                            _logger.DebugFormat("Leasing thread checking...HasLease: {0}", lease.HasLease);

                            while (lease.HasLease)
                            {
                                if (_leaseId != lease.LeaseId)
                                {
                                    _logger.DebugFormat("This instance ({0}) has the lease, updating blob with the instance ID.", AzureRoleEnvironment.CurrentRoleInstanceId());
                                    blob.Metadata["InstanceId"] = AzureRoleEnvironment.CurrentRoleInstanceId();
                                    blob.SetMetadata(lease.LeaseId);
                                    _leaseId = lease.LeaseId;
                                }

                                _resetEvent.WaitOne(TimeSpan.FromSeconds(10));
                                if (_cancellationToken.IsCancellationRequested)
                                    return;
                            }
                            if (!_cancellationToken.IsCancellationRequested)
                                _leaseId = null;
                        }

                        _resetEvent.WaitOne(TimeSpan.FromSeconds(30));
                        if (_cancellationToken.IsCancellationRequested)
                            return;
                    }
                    catch (Exception ex)
                    {
                        _logger.ErrorFormat(ex, "Failed to manage lease on {0}", AzureRoleEnvironment.CurrentRoleInstanceId());
                        _resetEvent.WaitOne(TimeSpan.FromSeconds(30));
                        if (_cancellationToken.IsCancellationRequested)
                            return;
                    }
                }
            }, _cancellationToken.Token);
        }