In the previous post I discussed how to implement sending messages. In this post I will show how to receive messages. The simplest code looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
static void GetMessage() { try { var queueClient = GetQueueClient(); var message = queueClient.Receive(); Console.WriteLine("Received a message: " + message.GetBody<string>()); } catch (Exception ex) { Console.WriteLine("An exception occured: " + ex.Message); } } private static QueueClient GetQueueClient(ReceiveMode receiveMode = ReceiveMode.ReceiveAndDelete) { const string queueName = "stockchangerequest"; var connectionString = ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"]; var queueClient = QueueClient.CreateFromConnectionString(connectionString, queueName, receiveMode); queueClient.RetryPolicy = new RetryExponential(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30), 10); return queueClient; } |
Program will now receive a message, parse body as string and output it on console. But messages should be read as they appear, so there need to… Continue reading Getting messages from Service Bus queue