You can Send Message to Queue or Read all the Message from Azure Queue using the following code blocks.
Sending Message to Azure Queue
using Microsoft.ServiceBus.Messaging; private async Task SendMessageToQueue(string message) { string serviceBusConnString = "****** Your Service bus Connection String ******"; string queueName = "Your Queue name"; QueueClient client = QueueClient.CreateFromConnectionString(serviceBusConnString, queueName, ReceiveMode.PeekLock); BrokeredMessage brokeredMessage = new BrokeredMessage(message); brokeredMessage.TimeToLive = new TimeSpan(3, 0, 0); await client.SendAsync(brokeredMessage); }
Reading all the Message from Azure Queue
using Microsoft.ServiceBus.Messaging; private async Task ReadMessagesFromQueue(string message) { string serviceBusConnString = "****** Your Service bus Connection String ******"; string queueName = "Your Queue name"; QueueClient client = QueueClient.CreateFromConnectionString(serviceBusConnString, queueName, ReceiveMode.PeekLock); if (client != null) { BrokeredMessage brokeredMessage = null; while (true) { try { //receive messages from Queue brokeredMessage = await client.ReceiveAsync(); if (brokeredMessage != null) { Console.WriteLine(string.Format("Message received: Id = {0}, Body = {1}", brokeredMessage.MessageId, brokeredMessage.GetBody())); // Further custom message processing could go here… brokeredMessage.Complete(); } else { //no more messages in the queue break; } } catch (MessagingException e) { if (!e.IsTransient) { Console.WriteLine(e.Message); throw; } else { Console.WriteLine(e.Message); Console.WriteLine("Transient error occured. Will retry in 2 seconds"); Thread.Sleep(2000); } } } client.Close(); } }
0 comments:
Post a Comment