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 publishesEventOneandEventTwoevents.NativeSubscriberA: an executable subscribing to theEventOneevent published by thePublisher.NativeSubscriberB: an executable subscribing to both events published by thePublisher.
Running the sample
- Start first the
NativeSubscriberAandNativeSubscriberBprojects. This will create all the necessary publish/subscribe infrastructure in Azure Service Bus, including the event topics. - In the
Publisherwindow, press any key to publish an event.- The endpoint in the
NativeSubscriberAwindow will receiveEventOne. - The endpoint in the
NativeSubscriberBwindow will receive bothEventOneandEventTwo.
- The endpoint in the
Setting up namespace entities
Each subscriber requires topic subscription(s) to receive the events published by the Publisher. The subscriptions are created on the EventOne or EventTwo topic, which follow the the default topic naming convention used by NServiceBus endpoints.
await client.CreateSubscriptionAsync(new CreateSubscriptionOptions(topicPath, subscriptionName)
{
LockDuration = TimeSpan.FromMinutes(5),
EnableDeadLetteringOnFilterEvaluationExceptions = false,
MaxDeliveryCount = int.MaxValue,
EnableBatchedOperations = true,
ForwardTo = forwardTo,
}, new CreateRuleOptions(ruleName, sqlFilter));
Subscription filters
Subscriptions created by NativeSubscriberA and NativeSubscriberB. NativeSubscriberA subscribes to the EventOne events by adding a subscription with the default TrueFilter and forwarding to its input queue:
await TopologyManager.CreateSubscription(
ConnectionString,
queueName,
ruleName:"$default",
sqlFilter: new TrueRuleFilter(),
topicPath: "EventOne",
forwardTo: queueName
);
The other subscriber creates two subscriptions following the same subscription pattern under both the EventOne and EventTwo topics.