static async Task SendMessageAsync(ServiceBusHttpMessage message, string token) { var address = $"https://{serviceBusNamespace}.servicebus.chinacloudapi.cn/{queueName}/messages"; HttpContent postContent = new ByteArrayContent(message.Body); // Serialize BrokerProperties. var brokerProps = JsonConvert.SerializeObject(message.SystemProperties, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore }); postContent.Headers.Add("BrokerProperties", brokerProps); // Add custom properties. foreach (string key in message.CustomProperties) { postContent.Headers.Add(key, message.CustomProperties.GetValues(key)); } var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Add("Authorization", token); httpClient.DefaultRequestHeaders.Add("ContentType", "application/atom+xml;type=entry;charset=utf-8"); // Send message. HttpResponseMessage response = null; try { response = await httpClient.PostAsync($"{address}?timeout=60", postContent); response.EnsureSuccessStatusCode(); Console.WriteLine("SendMessage successfully!"); } catch (HttpRequestException ex) { Console.WriteLine($"SendMessage failed: {ex.Message}"); } response.Dispose(); }
static async Task <ServiceBusHttpMessage> ResolveMessageFromResponse(HttpResponseMessage response) { // 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. string brokerProperties = headers.GetValues("BrokerProperties").First(); message.SystemProperties = JsonConvert.DeserializeObject <BrokerProperties>(brokerProperties); // 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); }
static void Main(string[] args) { var senderKeyName = "nameofkeywithsendpermission"; var senderKey = @"valueofkeywithsendpermission"; var receiverKeyName = "nameofkeywithreceivepermission"; var receiverKey = @"valueofkeywithreceivepermission"; // mooncake var queueUri = $"https://{serviceBusNamespace}.servicebus.chinacloudapi.cn/{queueName}"; // send message 1 Console.WriteLine("Sending message 1..."); var message1 = new ServiceBusHttpMessage(); var senderSasToken = createToken(queueUri, senderKeyName, senderKey); message1.Body = Encoding.UTF8.GetBytes("This is the first message."); message1.SystemProperties.Label = "M1"; message1.SystemProperties.TimeToLive = TimeSpan.FromSeconds(10); message1.CustomProperties.Add("Priority", "High"); message1.CustomProperties.Add("Customer", "12345"); message1.CustomProperties.Add("Customer", "ABC"); SendMessageAsync(message1, senderSasToken).Wait(); // receive message with ReceiveAndDelete mode Console.WriteLine("Receiving message 1 with ReceiveAndDelete mode..."); var receiverSasToken = createToken(queueUri, receiverKeyName, receiverKey); var receivedMsg = ReceiveMessageAsync(receiverSasToken).Result; if (receivedMsg != null) { Console.WriteLine($"ReceiveAndDeleted message: {Encoding.UTF8.GetString(receivedMsg.Body)} with id {receivedMsg.SystemProperties.MessageId}"); } // send message 2 Console.WriteLine("Sending message 2..."); var message2 = new ServiceBusHttpMessage(); message2.Body = Encoding.UTF8.GetBytes("This is the second message."); message2.SystemProperties.Label = "M2"; message2.SystemProperties.TimeToLive = TimeSpan.FromSeconds(20); message2.CustomProperties.Add("Priority", "Middle"); message2.CustomProperties.Add("User", "12345"); SendMessageAsync(message2, senderSasToken).Wait(); // receive message with PeekLock mode Console.WriteLine("Receiving message 2 with PeekLock mode..."); var receivedMsg2 = ReceiveMessageAsync(receiverSasToken, deleteMessage: false).Result; if (receivedMsg2 != null) { var msgId = receivedMsg2.SystemProperties.MessageId; var lockToken = receivedMsg2.SystemProperties.LockToken; var msgBody = Encoding.UTF8.GetString(receivedMsg2.Body); Console.WriteLine($"PeekAndLocked message: {msgBody} with id {msgId} and lock token {lockToken}"); DeleteMessageAsync(msgId, lockToken, receiverSasToken).Wait(); Console.WriteLine($"Completed message"); } Console.WriteLine("Press ENTER to exit!"); Console.ReadLine(); }