18

In Azure Service Bus, you can send a brokered message using QueueClient and MessageFactory. I would like to know why would you want to use one over the other.

Ralph Willgoss
  • 11,750
  • 4
  • 64
  • 67
Mitul
  • 9,734
  • 4
  • 43
  • 60

1 Answers1

26

Azure Service Bus provides different way to send/receive messages.

  • You can use the QueueClient to send and receive message to/from a queue.
  • You can use the TopicClient to send message to a topic
  • And you can use the SubscriptionClient to receive message from a subscription.

Using MessageSender and MessageReceiver, you create sender and receiver that are entity type invariant:

var factory = MessagingFactory.CreateFromConnectionString("MyConnectionString");
  • A MessageSender can send messages to both topic or queue:

    var sender = factory.CreateMessageSender("Queue ou topic path");
    
  • A MessageReceiver ca receive messages from both queue and subscription:

    var receiver = factory.CreateMessageReceiver("Queue ou subscription path");
    

Theses abstractions can give you more flexibility if you need to switch from a queue to a topic or vice versa because you just need to change the path of the service bus entity (This could be in your configuration file) so no code change needed. Using QueueClient, TopicClient, SubscriptionClient, you'll have to change your code if you want to move from a queue to a topic.

So my advice is to always use a MessageReceiver/MessageSender when you have to send/receive message from/to a an Azure ServiceBus queue topic/subscription.

NOTE: This does not apply for Eventhub which has a different implementation.

Frederik Struck-Schøning
  • 12,981
  • 8
  • 59
  • 68
Thomas
  • 24,234
  • 6
  • 81
  • 125
  • One note about MessagingFactory - it is not used to abstract message types. MessagingFactory is used to create both entity specific clients and message senders/receivers (entity type invariant approach). +1 on recommendation to use message sender/receiver when mixing entity types and operations. – Sean Feldman Aug 29 '16 at 15:18
  • 6
    If MessageReceivers/Senders do the job of Topic/Subscription/Queue Clients, plus they are more flexible and future-proof, why would one ever need xxxClients in the first place? What is the advantage of xxxClients over MessageReceivers/Senders? – Daniel B Apr 20 '17 at 01:22
  • 1
    In this [Best Practices](https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-performance-improvements) article, Microsoft says, MessagingFactory "also provides internal management of connections". Does this amount to connection pooling? I'm using externally-compiled Azure Functions and TopicClient re-connect on every call is a real bottleneck, but Functions don't have lifecycle events that permit calling CloseAsync on some sort of shutdown event. – McGuireV10 Nov 15 '17 at 01:09
  • @Daniel the documentation may have changed since 2017 :)... in 2019 it now states that xxxClients are created via the MessagingFactory (and, I assume, perform equally well): "Service Bus client objects, such as QueueClient or MessageSender, are created through a MessagingFactory" [reference](https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-performance-improvements#reusing-factories-and-clients) – Vince Horst Dec 12 '19 at 23:25