// Send a batch of messages.
        public async Task SendMessageBatch(string address, ServiceBusHttpMessage message, int timeoutInSeconds = 5)
        {
            // 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.");
            }

            var postContent = new ByteArrayContent(message.Body);

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

            HttpResponseMessage response = null;

            try
            {
                // Send message.
                response = await _httpClient.PostAsync(address + "/messages" + "?timeout=" + timeoutInSeconds, postContent);

                response.EnsureSuccessStatusCode();
            }
            catch (Exception ex)
            {
                if (response != null)
                {
                    if (response.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        throw new UnauthorizedAccessException(ex.Message);
                    }
                }
                throw;
            }
        }
        // Send a message.
        public async Task SendMessage(string address, ServiceBusHttpMessage message, int timeoutInSeconds = 5)
        {
            var postContent = new ByteArrayContent(message.Body);

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

            using (var ms = new MemoryStream())
            {
                serializer.WriteObject(ms, message.BrokerProperties);
                postContent.Headers.Add("BrokerProperties", Encoding.UTF8.GetString(ms.ToArray()));
            }

            // Add custom properties.
            foreach (string key in message.CustomProperties)
            {
                postContent.Headers.Add(key, message.CustomProperties.GetValues(key));
            }

            HttpResponseMessage response = null;

            try
            {
                // Send message.
                response = await _httpClient.PostAsync(address + "/messages" + "?timeout=" + timeoutInSeconds, postContent);

                response.EnsureSuccessStatusCode();
            }
            catch (Exception ex)
            {
                if (response != null)
                {
                    if (response.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        throw new UnauthorizedAccessException(ex.Message);
                    }
                }
                throw;
            }
        }
        public async Task <ServiceBusHttpMessage> Receive(string address, bool deleteMessage, int timeoutInSeconds = 60)
        {
            HttpResponseMessage response = null;

            try
            {
                // Retrieve message from Service Bus.
                if (deleteMessage)
                {
                    response = await _httpClient.DeleteAsync(address + "/messages/head?timeout=" + timeoutInSeconds);
                }
                else
                {
                    response = await _httpClient.PostAsync(address + "/messages/head?timeout=" + timeoutInSeconds, new ByteArrayContent(new byte[0]));
                }

                response.EnsureSuccessStatusCode();
            }
            catch (Exception ex)
            {
                if (response != null)
                {
                    if (response.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        throw new UnauthorizedAccessException(ex.Message);
                    }
                }
                throw;
            }

            // Check if a "valid" message was returned.
            var headers = response.Headers;

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

            // Get message body.
            var message = new ServiceBusHttpMessage
            {
                Body = await response.Content.ReadAsByteArrayAsync()
            };

            // Deserialize BrokerProperties.
            var brokerProperties = headers.GetValues("BrokerProperties");
            var serializer       = new DataContractJsonSerializer(typeof(BrokerProperties));

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

            // Get custom propoerties.
            foreach (var header in headers)
            {
                var key = header.Key;
                if (!key.Equals("Transfer-Encoding") && !key.Equals("BrokerProperties") && !key.Equals("ContentType") && !key.Equals("Location") && !key.Equals("Date") && !key.Equals("Server"))
                {
                    foreach (var value in header.Value)
                    {
                        var cleanValue = new string(value.Where(c => c != '\"').ToArray()); // strip out any quotes
                        message.CustomProperties.Add(key, cleanValue);
                    }
                }
            }

            // Get message URI.
            if (headers.Contains("Location"))
            {
                var locationProperties = headers.GetValues("Location");
                message.Location = locationProperties.FirstOrDefault();
            }
            return(message);
        }