예제 #1
0
        public void AddMessage(CloudQueueMessage message, Action<CloudOperationResponse<bool>> callback)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            var serviceOperation = string.Format(CultureInfo.InvariantCulture, "{0}/messages", this.Uri);
            var request = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(new Uri(serviceOperation));
            request.Headers["x-ms-version"] = "2009-09-19";
            request.Method = "POST";

            var xmlData = string.Format(CultureInfo.InvariantCulture, "<QueueMessage><MessageText>{0}</MessageText></QueueMessage>", Convert.ToBase64String(message.AsBytes));

            int encodedDataLength = Encoding.UTF8.GetByteCount(xmlData);

            this.StorageCredentials.SignRequest(request, encodedDataLength);

            request.BeginGetRequestStream(
                ar =>
                {
                    var postStream = request.EndGetRequestStream(ar);
                    var byteArray = Encoding.UTF8.GetBytes(xmlData);
                    postStream.Write(byteArray, 0, encodedDataLength);
                    postStream.Close();
                    request.BeginGetResponse(
                        asyncResult =>
                        {
                            try
                            {
                                var response = (HttpWebResponse)request.EndGetResponse(asyncResult);
                                if (response.StatusCode == HttpStatusCode.Created)
                                {
                                    callback(new CloudOperationResponse<bool>(true, null));
                                }
                                else
                                {
                                    var exceptionMessage = response.StatusCode == HttpStatusCode.BadRequest
                                        ? string.Format(CultureInfo.InvariantCulture, "Trying to add queue message too large to the queue '{0}'", this.Name)
                                        : string.Format(CultureInfo.InvariantCulture, "An error occurred while adding a message to the queue '{0}'", this.Name);

                                    callback(new CloudOperationResponse<bool>(false, new Exception(exceptionMessage)));
                                }
                            }
                            catch (Exception exception)
                            {
                                callback(new CloudOperationResponse<bool>(false, StorageClientExceptionParser.ParseXmlWebException(exception as WebException) ?? exception));
                            }
                        },
                        null);
                },
                request);
        }
예제 #2
0
        public void DeleteMessage(CloudQueueMessage message, Action<CloudOperationResponse<bool>> callback)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            var serviceOperation = string.Format(CultureInfo.InvariantCulture, "{0}/messages/{1}?popreceipt={2}", this.Uri, message.Id, System.Net.HttpUtility.UrlEncode(message.PopReceipt));

            var request = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(new Uri(serviceOperation));
            request.Headers["x-ms-version"] = "2009-09-19";
            request.Method = "DELETE";

            this.StorageCredentials.SignRequest(request, 0);
            request.BeginGetResponse(
                ar =>
                {
                    try
                    {
                        var response = (HttpWebResponse)request.EndGetResponse(ar);

                        if (response.StatusCode == HttpStatusCode.NoContent)
                        {
                            callback(new CloudOperationResponse<bool>(true, null));
                        }
                        else
                        {
                            var exceptionMessage = string.Format(CultureInfo.InvariantCulture, "An error occurred while deleting the queue named '{0}'", this.Name);

                            callback(new CloudOperationResponse<bool>(false, new Exception(exceptionMessage)));
                        }
                    }
                    catch (Exception exception)
                    {
                        callback(new CloudOperationResponse<bool>(false, StorageClientExceptionParser.ParseXmlWebException(exception as WebException) ?? exception));
                    }
                },
                null);
        }