In MassTransit, Topology is how message types are used to configure broker topics (exchanges in RabbitMQ) and queues. Topology is also used to access specific broker capabilities, such as RabbitMQ direct exchanges and routing keys.
Topology is separate from the send, publish, and consume pipelines which are focused more on middleware inside MassTransit. Topology allows conventions to be created that can apply message-specific topology configuration at runtime as messages are published and sent.
Once the bus is created, access to topology is via the Topology property on IBus. The message, publish, and send topologies are accessible on this interface. It is also possible to retrieve a message's publish address. The Topology property may support other interfaces, such as a transport-specific host topology. Pattern matching can be used to check the host topology type as shown below.
if (bus.Topology is IServiceBusBusTopology serviceBusTopology)
Topology does not cover sending messages beyond delivering messages to a queue. MassTransit sends messages via a send endpoint, which is retrieved using the endpoint's address only.
The exception to this is when the transport supports additional capabilities on send, such as the partitioning of messages. With RabbitMQ this would include specifying the
RoutingKey, and with Azure Service Bus this would include specifying the
PartitionId or the
Topology cannot alter the destination of a message, only the properties of the message delivery itself. Determining the path of a message is routing, which is handled separately.
Topology is a key part of publishing messages, and is responsible for how the broker's facilities are configured.
The publish topology defines many aspects of broker configuration, including:
- RabbitMQ Exchange names or Azure Service Bus Topic names
- Formatted, based upon the message type
- Explicit, based upon the configuration
- RabbitMQ Exchange Bindings or Azure Service Bus Topic Subscriptions
Publish is called, the topology is also used to:
- Populate the
RoutingKeyof the message sent to the RabbitMQ exchange
- Populate the
SessionIdof the message sent to the Azure Service Bus topic
Each receive endpoint has a consume topology, which is configured as consumers are added. Depending upon the transport, additional methods may be available to support exchange bindings, topic subscriptions, etc.
Consume topology uses the publish topology to ensure consistent naming of exchanges/topics for message types.