Exemple #1
0
        // Send a message.
        public async Task SendMessage(string address, ServiceBusHttpMessage message)
        {
            HttpContent postContent = new ByteArrayContent(message.body);

            // Serialize BrokerProperties.
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(BrokerProperties));
            MemoryStream ms = new MemoryStream();

            serializer.WriteObject(ms, message.brokerProperties);
            ms.Flush();
            byte[] proertyBytes = ms.ToArray();
            postContent.Headers.Add("BrokerProperties", Encoding.UTF8.GetString(proertyBytes, 0, proertyBytes.Length));

            ms.Dispose();
            // Add custom properties.
            foreach (string key in message.customProperties.Keys)
            {
                postContent.Headers.Add(key, message.customProperties[key]);
            }

            // Send message.
            HttpResponseMessage response = null;

            try
            {
                response = await this.httpClient.PostAsync(address + "/messages" + "?timeout=20", postContent);

                response.EnsureSuccessStatusCode();
            }
            catch (HttpRequestException ex)
            {
                this.sbConn.LogEvent(EventTypeConsts.Error, "SendMessage failed: ", ex.Message + "\n" + address);
            }
        }
Exemple #2
0
        // Send a batch of messages.
        public async Task SendMessageBatch(string address, ServiceBusHttpMessage message)
        {
            // Custom properties that are defined for the brokered message that contains the batch are ignored.
            // Throw exception to signal that these properties are ignored.
            if (message.customProperties.Count != 0)
            {
                throw new ArgumentException("Custom properties in BrokeredMessage are ignored.");
            }

            HttpContent postContent = new ByteArrayContent(message.body);

            postContent.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.microsoft.servicebus.json");

            // Send message.
            HttpResponseMessage response = null;

            try
            {
                response = await this.httpClient.PostAsync(address + "/messages" + "?timeout=20", postContent);

                response.EnsureSuccessStatusCode();
            }
            catch (HttpRequestException ex)
            {
                this.sbConn.LogEvent(EventTypeConsts.Error, "SendMessageBatch failed: ", ex.Message + "\n" + address);
            }
        }
        public async void ReadMessageAsync(string Location, int eventsExpiration)
        {
            try
            {
                ReaderMutex.WaitOne(MutexWaitTime); // Wait one minute for mutex

                ServiceBusHttpMessage receiveMessage = await HttpHelper.ReceiveAndDeleteMessage(this.subscriptionAddress);

                int countInBatch = 0;
                // Read messages in batches (10 messages or nothing)
                while (receiveMessage != null)
                {
                    countInBatch++;

                    string sBody = receiveMessage.GetMessageText();
                    if (!string.IsNullOrEmpty(sBody))
                    {
                        ProcessMessage(sBody, eventsExpiration);
                    }
                    else
                    {
                        /// Unknow schema - probably an error
                        this.LogEvent(EventTypeConsts.Error, "Unknown messagen format", sBody);
                    }

                    if (countInBatch < MaxMessageBatchCount)
                    {
                        receiveMessage = await HttpHelper.ReceiveAndDeleteMessage(this.subscriptionAddress);

                        if (receiveMessage != null)
                        {
                            /// wait a little bit before we'll process next message
                            await Task.Yield();
                        }
                    }
                    else
                    {
                        break; // Limith count of messages in single batch
                    }
                }
            }
            catch (Exception ex)
            {
                //  DC.Trace(ex.Message + ex.StackTrace);
                // some wrong message format - report and igore
                this.LogEvent(EventTypeConsts.Error, "Message error", ex.Message);
            }
            finally
            {
                ReaderMutex.ReleaseMutex();
            }
        }
Exemple #4
0
        public async Task <ServiceBusHttpMessage> Receive(string address, bool deleteMessage)
        {
            // Retrieve message from Service Bus.
            HttpResponseMessage response = null;

            try
            {
                if (deleteMessage)
                {
                    response = await this.httpClient.DeleteAsync(address + "/messages/head?timeout=20");
                }
                else
                {
                    response = await this.httpClient.PostAsync(address + "/messages/head?timeout=20", new ByteArrayContent(new Byte[0]));
                }
                response.EnsureSuccessStatusCode();
            }
            catch (HttpRequestException ex)
            {
                if (deleteMessage)
                {
                    this.sbConn.LogEvent(EventTypeConsts.Error, "ReceiveAndDeleteMessage failed: ", ex.Message + "\n" + address);
                }
                else
                {
                    this.sbConn.LogEvent(EventTypeConsts.Error, "ReceiveMessage failed: ", ex.Message + "\n" + address);
                }

                throw ex;
            }

            // Check if a message was returned.
            HttpResponseHeaders headers = response.Headers;

            if (!headers.Contains("BrokerProperties"))
            {
                return(null);
            }

            // Get message body.
            ServiceBusHttpMessage message = new ServiceBusHttpMessage();

            message.body = await response.Content.ReadAsByteArrayAsync();

            // Deserialize BrokerProperties.
            IEnumerable <string>       brokerProperties = headers.GetValues("BrokerProperties");
            DataContractJsonSerializer serializer       = new DataContractJsonSerializer(typeof(BrokerProperties));

            foreach (string key in brokerProperties)
            {
                using (MemoryStream ms = new MemoryStream(Encoding.GetEncoding("ASCII").GetBytes(key)))
                {
                    message.brokerProperties = (BrokerProperties)serializer.ReadObject(ms);
                }
            }

            // Get custom propoerties.
            foreach (var header in headers)
            {
                string key = header.Key;
                if (!key.Equals("Transfer-Encoding") && !key.Equals("BrokerProperties") && !key.Equals("ContentType") && !key.Equals("Location") && !key.Equals("Date") && !key.Equals("Server"))
                {
                    foreach (string value in header.Value)
                    {
                        message.customProperties.Add(key, value);
                    }
                }
            }

            // Get message URI.
            if (headers.Contains("Location"))
            {
                IEnumerable <string> locationProperties = headers.GetValues("Location");
                message.location = locationProperties.FirstOrDefault();
            }
            return(message);
        }