This sample shows how to subscribe to events published by 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
- NServiceBus endpoint that publishesEventOne
andEventTwo
events.NativeSubscriberA
- subscribes toEventOne
event published by thePublisher
.NativeSubscriberB
- subscribes to both events published by thePublisher
.
Setting up namespace entities
Each of the subscribers require 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 SubscriptionDescription(topicPath, subscriptionName)
{
LockDuration = TimeSpan.FromMinutes(5),
EnableDeadLetteringOnFilterEvaluationExceptions = false,
MaxDeliveryCount = int.MaxValue,
EnableBatchedOperations = true,
}, new RuleDescription(ruleName, sqlFilter)).ConfigureAwait(false);
Subscriptions filters
Subscriptions created by NativeSubscriberA
and NativeSubscriberB
both contain a single filtering rule. NativeSubscriberA
subscribes to the EventOne
events only by specifying sql subscripiton rule (event-one
) that matches the event type name stored in the event properites collection:
await TopologyManager.CreateSubscription(
ConnectionString,
subscriptionName,
ruleName:"event-one",
sqlFilter: new SqlFilter($"[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 it's subscription.
Things to note
- The use of the
AzureServiceBus_ConnectionString
environment variable mentioned above. - Execute
Publisher
first to ensure that the topic exists.