Azure Service bus with dotnet core

Working with Azure Service bus using dotnet core

Pricing information: MS Docs: Overview




NOTE Basic Tier cannot create topics!!!

Create and start subscriber

  1. Add package: Microsoft.Azure.ServiceBus (v3.1.0 as of today)
  2. Provide a connection string


  • TransportType = AmqpWebSockets is required when behind a proxy
  • EntityPath is required, set it to the name of the Topic when using topics
  • There needs to be an active subscription for the listener to start receiving messages
  1. Alternatively, compose a connection string using ServiceBusConnectionStringBuilder
  2. Use the ITopicClient to connect to a topic
ITopicClient topicClient = new TopicClient(ServiceBusConnectionString, TopicName);
// If behind proxy:
topicClient.ServiceBusConnection.TransportType = TransportType.AmqpWebSockets; 

Creating a Receiver daemon process

  1. Use Microsoft.Azure.ServiceBus.SubscriptionClient
using Microsoft.Azure.ServiceBus;

var connectionStringBuilder = new ServiceBusConnectionStringBuilder(connString);
var subscriptionName = "my-subscription";
var subscriber = new SubscriptionClient(connectionStringBuilder, subscriptionName)
  1. Register a message handler
// MyClassThatHandlesMessage is a type that has a Handle method
// with the signature: public Task Handle(Message message, CancellationToken cancellationToken)
var handler = new MyClassThatHandlesMessage();

subscriber.RegisterMessageHandler(async (message, cancellationToken) => {
    await handler.Handler(message, cancellationToken);

Stuff to think about

  1. When to trigger retry?
  • The design needs to take due care when catching and throwing exceptions.
  1. What happens to DeadLetter queues? Their is automatic retries for the number of retries configured, but what happens when a message fails even after retry?
  • If a subscriber was building eventual state based on message received, the internal state would not be accurate if messages are unprocessed (lying in the deadletter queue for instance)
  1. Configure number of retries, pre-fetch, lock duration and type