Esempio n. 1
0
        public bool AcquireLock(PessimisticConcurrencyContext lockContext)
        {
            var request = BlobRequest.Lease(this.GetUri(lockContext.ObjectId), BlobRequestTimeout, LeaseAction.Acquire, null);

            this.Account.Credentials.SignRequest(request);

            // add extra headers not supported by SDK - not supported by emulator yet (SDK 1.7)
            ////request.Headers["x-ms-version"] = "2012-02-12";
            ////request.Headers.Add("x-ms-lease-duration", lockContext.Duration.TotalSeconds.ToString());

            try
            {
                using (var response = request.GetResponse())
                {
                    if (response is HttpWebResponse &&
                        HttpStatusCode.Created.Equals((response as HttpWebResponse).StatusCode))
                    {
                        lockContext.LockId = response.Headers["x-ms-lease-id"];
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            catch (WebException e)
            {
                this._logger.LogWarning("Warning acquiring blob '{0}' lease: {1}", lockContext.ObjectId, e.Message);
                if (WebExceptionStatus.ProtocolError.Equals(e.Status))
                {
                    if (e.Response is HttpWebResponse)
                    {
                        if (HttpStatusCode.NotFound.Equals((e.Response as HttpWebResponse).StatusCode))
                        {
                            lockContext.LockId = null;
                            return(true);
                        }
                        else if (HttpStatusCode.Conflict.Equals((e.Response as HttpWebResponse).StatusCode))
                        {
                            lockContext.LockId = null;
                            return(false);
                        }
                    }
                    throw;
                }
                return(false);
            }
            catch (Exception e)
            {
                this._logger.LogError("Error acquiring blob '{0}' lease: {1}", lockContext.ObjectId, e.Message);
                throw;
            }
        }
Esempio n. 2
0
        public void ReleaseLock(PessimisticConcurrencyContext lockContext)
        {
            if (string.IsNullOrWhiteSpace(lockContext.LockId))
            {
                throw new ArgumentNullException("lockContext.LockId", "LockId cannot be null or empty");
            }

            var request = BlobRequest.Lease(this.GetUri(lockContext.ObjectId), BlobRequestTimeout, LeaseAction.Release, lockContext.LockId);

            this.Account.Credentials.SignRequest(request);

            using (var response = request.GetResponse())
            {
                if (response is HttpWebResponse &&
                    !HttpStatusCode.OK.Equals((response as HttpWebResponse).StatusCode))
                {
                    this._logger.LogError("Error releasing blob '{0}' lease: {1}", lockContext.ObjectId, (response as HttpWebResponse).StatusDescription);
                    throw new InvalidOperationException((response as HttpWebResponse).StatusDescription);
                }
            }
        }