Azure Service Bus .NET SDK Deep Dive – Sender, receiver and abstractions

Explains senders, receivers and the abstract types, for more posts in this series go to Contents.

So far we have been using QueueClient both for sending messages to a queue as well as receiving messages from a queue. The Azure Service Bus SDK has multiple concrete types that allow to operate with a specific entity. The QueueClient allows to interact with the abilities of a queue. If we want to leverage code that interacts with topics (more about those later) we can create a TopicClient. For interaction with subscriptions or sessions we can leverage SubscriptionClient or the SessionClient accordingly (also more about those later).

But when we are writing infrastructure code that needs to send messages to queues or topics or when we want to receive messages from queues or subscriptions we want to leverage a higher level abstraction that is capable of just sending a message or receiving messages to and from entities. For that purpose the Azure Service Bus SDK contains a MessageSender as well as a MessageReceiver abstraction. So that means instead of instantiating a QueueClient in the code shown in “Sending a message” we can simply create a MessageSender instance and point it to the queue as a destination.

var destination = "queue";
var client = new MessageSender(connectionString, destination);
var message = new Message();
message.Body = Encoding.UTF8.GetBytes("Deep Dive");
await client.SendAsync(message);
await client.CloseAsync();

Now the exact same code could be used send a message to a queue or send a message to a topic without needing to change the client type.

About the author

Daniel Marbach


By Daniel Marbach

Recent Posts