Azure Service Bus .NET SDK Deep Dive – Sending a message

Explains send a message to a queue, for more posts in this series go to Contents.

In the previous post we looked at how queues can be created with the management client. For being able to send messages to queues we required the AzureMessaging.ServiceBus package. If you search for Azure Service Bus on nuget you might stumble over the Microsoft.Azure.ServiceBus package, be aware that this is the old and new legacy Azure Service Bus client. For any new development you want to use the Azure.Messaging.ServiceBus client.

Once you have the connection string to Azure Service Bus you can start sending messages to queues by using the ServiceBusSender. The ServiceBusSender provides the necessary bits to interact with queues. Senders are created from a ServiceBusClient.

await using var serviceBusClient = new ServiceBusClient(connectionString);
await using var client = serviceBusClient.CreateSender(destination);
var message = new ServiceBusMessage(Encoding.UTF8.GetBytes("Deep Dive"));
await client.SendMessageAsync(message);

As you can see in the above snippet the constructor of the ServiceBusClient accepts the connection string. The destination queue is passed to the CreateSender method. Then we create the actual message, assign the body to it and use the client send method to send it to the destination. Once the send operations are done the client is closed via the provided async disposable.

For the sake of this example I’m using a simple approach to get the byte array for the message body by leveraging Encoding.UTF8.GetBytes. Imagine for production scenarios you might want to apply techniques like byte array buffering to reduce the allocations.

The message object allows setting more properties of the message. For example, in cases when metadata should be added without having to deserialize the payload it is possible to add that metadata to the ApplicationProperties.

message.ApplicationProperties.Add("TenantId", "MyTenantId");

Let’s see what happens when we execute the above code.

When trying to send a message to a destination that doesn’t exist a MessageEntityNotFoundException is raised. Let’s fix this briefly with the Service Bus Explorer tool (of course if you have read my previous post you would also know how to do this with code).

Creating the queue with Service Bus Explorer

After the destination queue is available the above code executes without any issues and the destination queue contains the message sent including all the user properties associated with the message.

Executing the same code again *fingers crossed*
The message has arrived

We have seen how it is possible with a few lines of code to send a message to an Azure Service Bus Queue using the ServiceBusSender. Next up we’ll see how it is possible to programmatically receive the messages out of the queue again.

Updated: 2021-03-23 to use the new SDK

About the author

Daniel Marbach

2 comments

By Daniel Marbach

Recent Posts