protected override void DoSave(IConcurrencyControlContext context, T obj)
        {
            if (string.IsNullOrWhiteSpace(context.ObjectId))
            {
                throw new ArgumentNullException("context.ObjectId", "ObjectId cannot be null or empty");
            }

            if (context is OptimisticConcurrencyContext)
            {
                CloudBlob blob = this.Container.GetBlobReference(context.ObjectId);
                blob.Properties.ContentType = "application/json";

                var blobRequestOptions = new BlobRequestOptions()
                {
                    AccessCondition = (context as OptimisticConcurrencyContext).AccessCondition
                };

                blob.UploadText(new JavaScriptSerializer().Serialize(obj), Encoding.Default, blobRequestOptions);
            }
            else if (context is PessimisticConcurrencyContext)
            {
                if (string.IsNullOrWhiteSpace((context as PessimisticConcurrencyContext).LockId))
                {
                    throw new ArgumentNullException("context.LockId", "LockId cannot be null or empty");
                }

                var blobProperties = new BlobProperties();
                blobProperties.ContentType = "application/json";

                var updateText = BlobRequest.Put(
                    this.GetUri(context.ObjectId),
                    BlobRequestTimeout,
                    blobProperties,
                    BlobType.BlockBlob,
                    (context as PessimisticConcurrencyContext).LockId,
                    0);
                using (var stream = new StreamWriter(updateText.GetRequestStream(), Encoding.Default))
                {
                    stream.Write(new JavaScriptSerializer().Serialize(obj));
                }
                this.Account.Credentials.SignRequest(updateText);

                using (var response = updateText.GetResponse())
                {
                    if (response is HttpWebResponse &&
                        !HttpStatusCode.Created.Equals((response as HttpWebResponse).StatusCode))
                    {
                        TraceHelper.TraceError("Error writing leased blob '{0}': {1}", context.ObjectId, (response as HttpWebResponse).StatusDescription);
                        throw new InvalidOperationException((response as HttpWebResponse).StatusDescription);
                    }
                }
            }
            else
            {
                throw new InvalidOperationException("IConcurrencyControlContext implementation cannot be handled");
            }
        }
Exemplo n.º 2
0
        protected virtual void OptimisticControlContextWriteStrategy(IConcurrencyControlContext context, T obj)
        {
            CloudBlob blob = this.Container.GetBlobReference(context.ObjectId);

            var blobRequestOptions = new BlobRequestOptions()
            {
                AccessCondition = (context as OptimisticConcurrencyContext).AccessCondition
            };

            this.WriteObject(blob, blobRequestOptions, obj);
        }
Exemplo n.º 3
0
        protected virtual void PessimisticControlContextWriteStrategy(IConcurrencyControlContext context, T obj)
        {
            string lockId = (context as PessimisticConcurrencyContext).LockId;
            string objId  = (context as PessimisticConcurrencyContext).ObjectId;

            if (string.IsNullOrWhiteSpace(lockId))
            {
                throw new ArgumentNullException("context.LockId", "LockId cannot be null or empty");
            }

            CloudBlockBlob blob            = this.Container.GetBlockBlobReference(objId);
            var            accessCondition = AccessCondition.GenerateLeaseCondition(lockId);

            this.WriteOject(blob, null, accessCondition, obj);
        }
Exemplo n.º 4
0
        public virtual void Save(IConcurrencyControlContext context, T obj)
        {
            if (string.IsNullOrWhiteSpace(context.ObjectId))
            {
                throw new ArgumentNullException("context.ObjectId", "ObjectId cannot be null or empty");
            }

            Action <IConcurrencyControlContext, T> writeStrategy;

            if (!this._writingStrategies.TryGetValue(context.GetType(), out writeStrategy))
            {
                throw new InvalidOperationException("IConcurrencyControlContext implementation not registered");
            }

            this.StorageRetryPolicy.ExecuteAction(() => writeStrategy(context, obj));
        }
Exemplo n.º 5
0
        protected virtual void PessimisticControlContextWriteStrategy(IConcurrencyControlContext context, T obj)
        {
            if (string.IsNullOrWhiteSpace((context as PessimisticConcurrencyContext).LockId))
            {
                throw new ArgumentNullException("context.LockId", "LockId cannot be null or empty");
            }

            var blobProperties = new BlobProperties();

            var binarizedObject = this.BinarizeObjectForStreaming(blobProperties, obj);

            var updateRequest = BlobRequest.Put(
                this.GetUri(context.ObjectId),
                BlobRequestTimeout,
                blobProperties,
                BlobType.BlockBlob,
                (context as PessimisticConcurrencyContext).LockId,
                0);

            using (var writer = new BinaryWriter(updateRequest.GetRequestStream(), Encoding.Default))
            {
                writer.Write(binarizedObject);
            }

            this.Account.Credentials.SignRequest(updateRequest);

            using (var response = updateRequest.GetResponse())
            {
                if (response is HttpWebResponse &&
                    !HttpStatusCode.Created.Equals((response as HttpWebResponse).StatusCode))
                {
                    this._logger.LogError("Error writing leased blob '{0}': {1}", context.ObjectId, (response as HttpWebResponse).StatusDescription);
                    throw new InvalidOperationException((response as HttpWebResponse).StatusDescription);
                }
            }
        }
Exemplo n.º 6
0
        protected virtual void OptimisticControlContextWriteStrategy(IConcurrencyControlContext context, T obj)
        {
            CloudBlockBlob blob = this.Container.GetBlockBlobReference(context.ObjectId);

            this.WriteOject(blob, null, (context as OptimisticConcurrencyContext).AccessCondition, obj);
        }
 protected abstract void DoSave(IConcurrencyControlContext context, T obj);
 public virtual void Save(IConcurrencyControlContext context, T obj)
 {
     this.StorageRetryPolicy.ExecuteAction(() => this.DoSave(context, obj));
 }
 protected override void DoSave(IConcurrencyControlContext context, byte[] obj)
 {
     throw new NotImplementedException();
 }