Service Bus subscriptions - ARM client vs ServiceBusAdministrationClient

Provisioning Azure Service Bus subscriptions under a topic doesn't sound like a big deal. However, Azure offers two SDKs: the Service Bus Management and Messaging libraries. Both can create subscriptions. Which library is the right one?

Management Library

The management library for ASB is available via the Azure.ResourceManager.ServiceBus package. It operates on the ARM (Azure Resource Manager) level. It's important to remember that this library operates on each entity as an individual resource. Creating a subscription is a separate operation from creating a rule. When a subscription is created first, it will, in fact, have a default SQL rule, 1=1. While for most greenfield scenarios, this might not be an issue, for existing systems, this might pose a challenge. If there are already published messages, and we're only interested in a subset of those messages, then for the time between the subscription resource creation and the rule resource creation, there's a time window where the default rule will be in place, allowing accepting all messages to be received by the subscription queue. And that's a problem as receiving unplanned message types could cause the process to fail until the unwanted messages are drained.

Messaging Library

Messaging library, in general, is intended for message processing, but the creation of subscriptions and rules modifications is supported by the ServiceBusAdministrationClient found in the package Azure.Messaging.ServiceBus. When using the management client, combining the subscription provisioning with its default rule is possible.

var admin = new ServiceBusAdministrationClient("<fully-qualified-namespace>", new DefaultAzureCredential());

await admin.CreateSubscriptionAsync(
	new CreateSubscriptionOptions("topic", "subscription"),
	new CreateRuleOptions("rule", new SqlRuleFilter("\"message-type\"='PageViewed'")) // SQL or Correlation filter
);

With this approach, the subscription and the rule are created simultaneously, eliminating any chance of receiving unaccounted-for message types.

Note: To perform these operations, you must have Manage rights to the Service Bus namespace. When using Azure Identity, the Service Bus Data Owner role must be granted to your identity.

No Comments

Add a Comment

As it will appear on the website

Not displayed

Your website