AI agents: use the documentation index at llms.txt to locate machine-readable pages. This section is indexed by https://docs.particular.net/samples/llms.txt. The markdown version of this page is served as plain text. An MCP server at /mcp serves the same content via the search_docs and read_doc tools; it is read-only and needs no credentials. Markdown versions of documentation pages are available by appending .md to the page URL. Directory URLs use index.md. They are served as text/plain because some retrieval backends reject text/markdown.

Azure Service Bus Pub/Sub Native Integration

NuGet Package:
NServiceBus.Transport.AzureServiceBus 4.x
Target Version:
NServiceBus 9.x

This sample shows how to subscribe to events published by an NServiceBus endpoint using the Azure Service Bus API.

Prerequisites

An environment variable named AzureServiceBus_ConnectionString with the connection string for the Azure Service Bus namespace.

Code walk-through

The sample contains three executable projects:

  • Publisher: an NServiceBus endpoint that publishes EventOne and EventTwo events.
  • NativeSubscriberA: an executable subscribing to the EventOne event published by the Publisher.
  • NativeSubscriberB: an executable subscribing to both events published by the Publisher.

Running the sample

  1. First, run the Publisher project by itself. This will create all the necessary publish/subscribe infrastructure in Azure Service Bus, including the default bundle-1 topic.
  2. Run the projects normally so that all endpoints start.
  3. In the Publisher window, press any key to publish an event.
    • The endpoint in the NativeSubscriberA window will receive EventOne.
    • The endpoint in the NativeSubscriberB window will receive both EventOne and EventTwo.

Setting up namespace entities

Each subscriber requires a topic subscription to receive the events published by the Publisher. The subscriptions are created on the bundle-1 topic, which is the default name used by NServiceBus endpoints.

await client.CreateSubscriptionAsync(new CreateSubscriptionOptions(topicPath, subscriptionName)
{
    LockDuration = TimeSpan.FromMinutes(5),
    EnableDeadLetteringOnFilterEvaluationExceptions = false,
    MaxDeliveryCount = int.MaxValue,
    EnableBatchedOperations = true,
}, new CreateRuleOptions(ruleName, sqlFilter));

Subscription filters

Subscriptions created by NativeSubscriberA and NativeSubscriberB contain a single filtering rule. NativeSubscriberA subscribes to the EventOne events only by specifying a SQL subscription rule (event-one) that matches the event type name stored in the event properties collection:

await TopologyManager.CreateSubscription(
    ConnectionString,
    subscriptionName,
    ruleName: "event-one",
    sqlFilter: new SqlRuleFilter($"[NServiceBus.EnclosedMessageTypes] LIKE '%{typeof(EventOne).FullName}%'")
    );

The other subscriber uses TrueFilter in the all-events rule which ensures that both EventOne and EventTwo events are routed to its subscription.

Related Articles