Namespace hierarchy support

Component: Azure Service Bus Transport | Nuget: NServiceBus.Azure.Transports.WindowsAzureServiceBus (Version: 7.x)
Target NServiceBus Version: 6.x

At the core of the Azure Service Bus service, there is a Service Registry which tracks the location of each queue, topic, relay or eventhub in the service. This Service Registry provides a DNS integrated hierarchical naming system, that has a root entry point (called a namespace) at a URI with the following scheme.

https://{serviceNamespace}.servicebus.windows.net/{path}

The parameter can be specified at any depth, allowing the hierarchy to be extended with application specific sub trees. This is very useful for large systems to group, manage and secure resources more efficiently, for example:

https://mynamespace.servicebus.Windows.net/production/tenant1/sales
https://mynamespace.servicebus.Windows.net/production/tenant1/operations
https://mynamespace.servicebus.Windows.net/acceptance/tenant1/sales
https://mynamespace.servicebus.Windows.net/acceptance/tenant1/operations

For more information about this feature refer to the Addressing and Protocol article on MSDN.

Positioning an endpoint in the hierarchy

NServiceBus provides to an endpoint the capability to register its Azure Service Bus transport resources inside a namespace hierarchy by replacing part of the addressing logic - the composition strategy. The composition strategy is responsible for determining the path to an entity in the namespace. Out of the box, the transport comes with two implementations - FlatComposition and HierarchyComposition strategies. The FlatComposition strategy is the default and applies no composition logic, resulting in a flat namespace hierarchy. The HierarchyComposition strategy allows to specify a lambda expression that can calculate the path for each entity.

Edit
var transport = endpointConfiguration.UseTransport<AzureServiceBusTransport>();
var composition = transport.Composition();
var hierarchy = composition.UseStrategy<HierarchyComposition>();
hierarchy.PathGenerator(
    pathGenerator: entityName =>
    {
        return "production/tenant1/";
    });

Note that the path generator is a lambda function, so it will be invoked each time the transport wants to determine the location of a given entity. This function must meet the following rules:

  • Return the path to the entity.
  • Does not appending the entity name itself.
  • Does not append a trailing slash (/).

Implementing a custom composition strategy

It is also possible to provide a custom composition strategy by implementing ICompositionStrategy, which might be beneficial for example with regards to performance.

Edit
class CustomComposition :
    ICompositionStrategy
{
    public string GetEntityPath(string entityname, EntityType entityType)
    {
        return "path/to/entity";
    }
}

The implementation of the ICompositionStrategy needs to be registered:

Edit
var transport = endpointConfiguration.UseTransport<AzureServiceBusTransport>();
var composition = transport.Composition();
composition.UseStrategy<CustomComposition>();

Last modified