Prerequisites
An environment variable named AzureServiceBus_ConnectionString with the connection string for the Azure Service Bus namespace.
Code walk-through
This sample shows a hierarchy namespace with the Azure Service Bus transport. In the sample, there are three endpoints:
HierarchyClient- belongs to a hierarchy (my-hierarchy), and sends messages and publishes an eventHierarchyEndpoint- belongs to the same hierarchy, and handles all commands and events, but should only receive those from the hierarchyExternalEndpoint- outside of the hierarchy, and handles all commands and events, but should only receive those excluded from the hierarchy
The HierarchyClient sends the following messages
- Commands
HierarchyCommandExternalCommand
- Events
HierarchyEvent,ExternalEventandOtherExternalEvent(both implement theIExternalEventinterface)
Transport configuration
The two hierarchy endpoints (HierarchyClient and HierarchyEndpoint) are configured to be included in the hierarchy with the following configuration:
transport.HierarchyNamespaceOptions = new HierarchyNamespaceOptions { HierarchyNamespace = "my-hierarchy" };
The ExternalEndpoint is not included in the hierarchy.
Escaping the hierarchy
Messages can be configured to be sent outside of the hierarchy using the ExcludeMessageType method on the HierarchyNamespaceOptions property of the transport:
// Exclude a single concrete type
transport.HierarchyNamespaceOptions.ExcludeMessageType<ExternalCommand>();
// Exclude all types by inheritance
transport.HierarchyNamespaceOptions.ExcludeMessageType<IExternalEvent>();
The command is excluded explicitly (by using its concrete type), and the events are excluded using their shared interface (so that one line excludes all events inheriting IExternalEvent).
Scenario 1 - hierarchy command
HierarchyClient sends a HierarchyCommand to both endpoints. It is sent successfully to HierarchyEndpoint because it belongs to the same hierarchy. It is not sent to ExternalEndpoint because it is outside the hierarchy, and the expected queue does not exist(my-hierarchy/ should not be prefixed with my-hierarchy\). The following message is included in the exception:
The messaging entity 'sb:/
Scenario 2 - excluded command
HierarchyClient sends an ExternalCommand to both receivers. It is sent successfully to ExternalEndpoint because this type of message is excluded from the hierarchy, and ExternalEndpoint is outside the hierarchy. It is not sent to HierarchyEndpoint, because it is in the hierarchy, and the expected queue does not exist (Samples. should be prefixed with my-hierarchy/). The following message is included in the exception:
The messaging entity 'sb:/
Scenario 3 - hierarchy event
HierarchyClient publishes a HierarchyEvent. Both endpoints are subscribed to this event. Only HierarchyEndpoint receives it because it is in the same hierarchy as HierarchyClient. Since ExternalEndpoint is outside the hierarchy, the event is not delivered to it.
Scenario 4 - excluded events
HierarchyClient publishes both ExternalEvent and OtherExternalEvent events. Both endpoints are subscribed to these events. Only ExternalEndpoint receives them because it is outside of the my-hierarchy hierarchy, and these events have been excluded.
Viewing messages in-flight
The following queues and topics for the endpoints can be seen in the Azure Portal or a third-party tool:
Queues
my-hierarchy/samples. asbs. hierarchynamespaceescape. hierarchyclient my-hierarchy/samples. asbs. hierarchynamespaceescape. hierarchyendpoint samples.asbs. hierarchynamespaceescape. externalendpoint my-hierarchy/error error
Topics
my-hierarchy/shared. hierarchyevent my-hierarchy/shared. externalevent my-hierarchy/shared. otherexternalevent hierarchyeventshared.externalevent shared.otherexternalevent