Exemplo n.º 1
0
        public void UpdateAsync(SsdsBlobEntity blob, ConcurrencyPattern concurrencyPattern, BlobOperationComplete onComplete)
        {
            Exception        exception = null;
            BackgroundWorker worker    = new BackgroundWorker();

            worker.DoWork += new DoWorkEventHandler(delegate(object sender, DoWorkEventArgs e)
            {
                try
                {
                    this.Update(blob, concurrencyPattern);
                }
                catch (Exception ex)
                {
                    exception = ex;
                }
            });

            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object sender, RunWorkerCompletedEventArgs e)
            {
                if (onComplete != null)
                {
                    onComplete(blob, exception);
                }
            });

            worker.RunWorkerAsync();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Updates the item on the container.
        /// </summary>
        /// <param name="entity">The item with the information to be updated.</param>
        public void Update <T>(SsdsEntity <T> entity, ConcurrencyPattern concurrencyPattern) where T : class, new()
        {
            Uri                      updateLocation = HttpRestUriTemplates.UpdateTemplate.BindByPosition(this.authority, this.container, entity.Id);
            SsdsRestFacade           facade         = this.CreateFacade(updateLocation);
            SsdsEntitySerializer <T> serializer     = new SsdsEntitySerializer <T>();
            string                   payload        = serializer.Serialize(entity);

            facade.Update(payload, concurrencyPattern, entity.Version.ToString());
        }
Exemplo n.º 3
0
        /// <summary>
        /// Sends a Put message to the current container using the paylod as the message body.
        /// </summary>
        /// <param name="payload">The message body.</param>
        public void Update(string payload, ConcurrencyPattern concurrencyPattern, string version)
        {
            WebRequest request = this.CreateRequest(this.scope, HttpVerbs.Put, payload);

            if (concurrencyPattern == ConcurrencyPattern.IfMatch)
            {
                request.Headers.Add("if-match", version);
            }
            else if (concurrencyPattern == ConcurrencyPattern.IfNoneMatch)
            {
                request.Headers.Add("if-none-match", version);
            }

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    string message = string.Format(CultureInfo.InvariantCulture, "Unexpected return code: {0}", response.StatusCode);
                    throw new InvalidOperationException(message);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Sends a Delete message to the current container using the entityId
        /// as the last fragment of the Uri.
        /// </summary>
        /// <param name="entityId">The identifier for the object where the message is going.</param>
        public void Delete(string entityId, ConcurrencyPattern concurrencyPattern, string version)
        {
            Uri        destination = HttpRestUriTemplates.EntityTemplate.BindByPosition(this.scope, entityId);
            WebRequest request     = this.CreateRequest(destination, HttpVerbs.Delete);

            if (concurrencyPattern == ConcurrencyPattern.IfMatch)
            {
                request.Headers.Add("if-match", version);
            }
            else if (concurrencyPattern == ConcurrencyPattern.IfNoneMatch)
            {
                request.Headers.Add("if-none-match", version);
            }

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    string message = string.Format(CultureInfo.InvariantCulture, "Unexpected return code: {0}", response.StatusCode);
                    throw new InvalidOperationException(message);
                }
            }
        }
Exemplo n.º 5
0
        private string InsertOrUpdate(SsdsBlobEntity blob, string verb, ConcurrencyPattern concurrencyPattern)
        {
            string       locationResult  = String.Empty;
            IAsyncResult asyncResult     = null;
            Exception    backgroundError = null;

            blob.InitRead();

            Uri containerScope = this.scope;

            if (verb == HttpVerbs.Put)
            {
                containerScope = new Uri(String.Format(CultureInfo.InvariantCulture, "{0}/{1}", this.scope, blob.Id));
            }

            HttpWebRequest uploadRequest = CreateStreamingRequest(containerScope, verb, blob);

            if (verb == HttpVerbs.Post)
            {
                uploadRequest.Headers["Slug"] = blob.Id;
            }

            if (concurrencyPattern == ConcurrencyPattern.IfMatch)
            {
                uploadRequest.Headers.Add("if-match", blob.Version);
            }
            else if (concurrencyPattern == ConcurrencyPattern.IfNoneMatch)
            {
                uploadRequest.Headers.Add("if-none-match", blob.Version);
            }

            try
            {
                using (Stream requestStream = uploadRequest.GetRequestStream())
                {
                    while (blob.ReadBuffer() > 0)
                    {
                        requestStream.Write(blob.Buffer, 0, blob.ReadSize);
                        if (asyncResult == null)
                        {
                            asyncResult = uploadRequest.BeginGetResponse((IAsyncResult res) =>
                            {
                                HttpWebRequest req = (HttpWebRequest)res.AsyncState;
                                try
                                {
                                    using (HttpWebResponse response = (HttpWebResponse)req.EndGetResponse(res))
                                    {
                                        locationResult = response.Headers[HttpResponseHeader.Location];
                                    }
                                }
                                catch (Exception ex)
                                {
                                    backgroundError = ex;
                                }
                            }, uploadRequest);
                        }
                    }
                }

                if (asyncResult != null)
                {
                    asyncResult.AsyncWaitHandle.WaitOne();
                    if (backgroundError != null)
                    {
                        throw backgroundError;
                    }
                }
            }
            catch (WebException ex)
            {
                string errorMsg = ex.Message;

                if (ex.Response != null)
                {
                    using (HttpWebResponse errorResponse = (HttpWebResponse)ex.Response)
                    {
                        errorMsg = ReadResponse(errorResponse);
                    }
                }

                throw new ApplicationException(errorMsg);
            }

            return(locationResult);
        }
Exemplo n.º 6
0
 public void Update(SsdsBlobEntity blob, ConcurrencyPattern concurrencyPattern)
 {
     this.InsertOrUpdate(blob, HttpVerbs.Put, concurrencyPattern);
 }
Exemplo n.º 7
0
        public void Update(SsdsBlobEntity blob, ConcurrencyPattern concurrencyPattern)
        {
            SsdsRestFacade facade = this.CreateFacade();

            facade.Update(blob, concurrencyPattern);
        }
Exemplo n.º 8
0
 /// <summary>
 /// Updates the entity with the given concurrency pattern
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="item"></param>
 /// <param name="id"></param>
 /// <param name="pattern"></param>
 public void Update <T>(T item, string id, ConcurrencyPattern pattern) where T : class, new()
 {
     Update(new SsdsEntity <T> {
         Id = id, Entity = item
     }, pattern);
 }
Exemplo n.º 9
0
        private void Delete(string id, ConcurrencyPattern concurrencyPattern, string version)
        {
            SsdsRestFacade facade = this.CreateFacade();

            facade.Delete(id, concurrencyPattern, version);
        }
Exemplo n.º 10
0
 public void Delete(SsdsBlobEntity entity, ConcurrencyPattern concurrencyPattern)
 {
     this.Delete(entity.Id, concurrencyPattern, entity.Version);
 }
Exemplo n.º 11
0
 public void Delete <T>(SsdsEntity <T> entity, ConcurrencyPattern concurrencyPattern) where T : class, new()
 {
     this.Delete(entity.Id, concurrencyPattern, entity.Version.ToString());
 }