Creating Azure Service Bus Queues And Sending Messages To Queues

Service Bus


The rationale behind the usage of service bus is to decouple functions and companies. As we have to decouple functions and companies, the information could be transferred between completely different functions and companies utilizing messages. The messages could be despatched to the queue within the service bus.


Creating Service Bus Queue


Most of you would possibly conscious easy methods to create a service bus queue in  Azure, however let me overview.


Manner 1


Login tothe Azure portal and create a service bus, namespace. As soon as that’s executed, you possibly can see the queues and matters within the left hand aspect. As soon as we click on on queue, we are able to click on on Add and create a queue from Azure portal.



Manner 2


If we now have service Bus explorer, we are able to login into the service bus explorer with the connection string of service bus and proper click on on the queue and create the queue. We are able to have the settings enabled for the queue.


Creating Azure Service Bus Queues And Sending Messages To Queues


Among the helpful settings are:


Allow Classes


This permits a assure of processing the messages in a sequential method. This follows the sample of FIFO.


Enabled Lifeless Lettering


This permits the messages to be in deadletter if the receiver fails to obtain the messages in time. The message won’t expire in deadletter till and except it’s considered.


Configuring the Service Bus queue within the growth surroundings.


Set up the Microsoft.Azure.ServiceBus nuget bundle to the venture the place you’re organising the message queue.


First let’s create a category for configuring the QueueClient and have a generic technique for sending the message to the queue.

  1. utilizing Azure.ServiceBus;  
  2. utilizing Microsoft.Azure.ServiceBus;  
  3. utilizing System;  
  4. utilizing System.Collections.Generic;  
  5. utilizing System.Textual content;  
  6. namespace AzureFunctionsUnitTesting.Service_Bus {  
  7.     public class QueueSender: IQueueSender {  
  8.         static Dictionary < string, IQueueClient > _queueClients;  
  9.         public QueueSender(string connectionString, Record < Queue > queues) {  
  10.             _queueClients = new Dictionary < string, IQueueClient > ();  
  11.             if (!string.IsNullOrEmpty(connectionString)) {  
  12.                 foreach(Queue queue in queues) {  
  13.                     string queueId = queue.Id;  
  14.                     string queueName = queue.Identify;  
  15.                     _queueClients.Add(queueId, new QueueClient(connectionString, queueName));  
  16.                 }  
  17.             };  
  18.         }  
  19.         public async void SendMessageAsync(string queueName, string messageItem, string messageLabel = null, Dictionary < stringobject > messageProperties = null) {  
  20.             attempt {  
  21.                 Message message = BuildMessage(messageItem, messageLabel, messageProperties);  
  22.                 await _queueClients[queueName].SendAsync(message);  
  23.             } catch (Exception ex) {  
  24.                 throw ex;  
  25.             }  
  26.         }  
  27.         personal Message BuildMessage(string messageItem, string messageLabel, Dictionary < stringobject > messageProperties) {  
  28.             Message message = new Message(Encoding.UTF8.GetBytes(messageItem));  
  29.             message.UserProperties[“TimeStampUTC”] = DateTime.UtcNow;  
  30.             message.Label = messageLabel;  
  31.             if (messageProperties != null) {  
  32.                 foreach(var messageProperty in messageProperties) {  
  33.                     message.UserProperties[messageProperty.Key] = messageProperty.Worth;  
  34.                 }  
  35.             }  
  36.             return message;  
  37.         }  
  38.     }  
  39. }  

I’ve configured the QueueClient within the constructor in order that it is going to be loaded when ever we inject the category by way of dependency injection.


I’ve created a technique (BuildMessage) to construct the message which will likely be despatched to the queue. Message is a category of Azure service bus Nugets.


As soon as the message is constructed, we’ll ship the message by way of the queue shopper we now have injected by making a Technique(SendMessageAsync). The queue shopper will care for sending the message to the service bus queue.


Calling the Configured Queue Sender for sending our customized Message


I’ve created a category and injected the Queue sender interface. I’ve created a technique and will likely be calling the sendMessageAsync Technique to ship the message.

  1. utilizing AzureFunctionsUnitTesting.Interfaces;  
  2. utilizing AzureFunctionsUnitTesting.Service_Bus;  
  3. utilizing Microsoft.Azure.ServiceBus;  
  4. namespace AzureFunctionsUnitTesting {  
  5.     public class MessageQueueSender: IMessageQueueSender {  
  6.         personal readonly IQueueSender _queueSender;  
  7.         public MessageQueueSender(IQueueSender queueSender) {  
  8.             _queueSender = queueSender;  
  9.         }  
  10.         public void SendMessageToQueue() {  
  11.             Message message = new Message();  
  12.             message.SessionId = “session1”;  
  13.             string queueName = “testqueue”;  
  14.             string content material = “Hey Service Bus Queue”;  
  15.             _queueSender.SendMessageAsync(queueName, content material);  
  16.         }  
  17.     }  
  18. }  

Unit Testing for Sending Message to Service Bus Queue


We are able to take a look at if the configuration for organising the service bus queue is appropriate or not by making a unit take a look at case.


I’ve created a unit take a look at case the place I will likely be mocking up all of the required configuration and will likely be calling the SendMessageToQueue technique and checking the rely of messages within the queue as assertion.

  1. utilizing AzureFunctionsUnitTesting.Service_Bus;  
  2. utilizing Microsoft.VisualStudio.TestTools.UnitTesting;  
  3. utilizing Moq;  
  4. utilizing System.Collections.Generic;  
  5. namespace AzureFunctionsUnitTesting.Checks {  
  6.     [TestClass]  
  7.     public class MessageQueueSenderTests {  
  8.         public readonly Mock < IQueueSender > _queueSender;  
  9.         public Record < string > _queueMessages;  
  10.         public MessageQueueSenderTests() {  
  11.                 _queueSender = new Mock < IQueueSender > ();  
  12.                 _queueMessages = new Record < string > ();  
  13.                 _queueSender.Setup(x => x.SendMessageAsync(It.IsAny < string > (), It.IsAny < string > (), It.IsAny < string > (), It.IsAny < Dictionary < stringobject >> ())).Callback < stringstringstring, Dictionary < stringobject >> (async (a, b, d, e) => {  
  14.                     _queueMessages.Add($ “{a}”);  
  15.                 });  
  16.             }  
  17.             [TestInitialize()]  
  18.         public digital void Initialise() {}  
  19.             [TestMethod]  
  20.         public void SendMessageToQueue() {  
  21.             MessageQueueSender messageQueueSender = new MessageQueueSender(_queueSender.Object);  
  22.             messageQueueSender.SendMessageToQueue(“testservicebus”“Hey ServiceBus”“sessionId1”);  
  24.             Assert.AreEqual(1, _queueMessages.Depend, “Message sending failed”);  
  25.         }  
  26.     }  
  27. }  

I hope this text is useful for everybody and please submit your questions with something associated to service bus queue and matters. I’ve connected the answer file for reference.

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also