Conventions
A convention is a class that Mocha calls for each configuration of one kind while it builds the bus. It receives the configuration of a message type or an endpoint and can change it before Mocha uses it. Mocha assigns the default urn:message: identity of message types with a built-in convention.
Use a convention when a setting applies to many message types or endpoints. To configure a single message type or endpoint, use AddMessage<T>() or the endpoint descriptor instead.
Write a convention#
A convention implements the interface for the configuration it changes. This convention limits every receive endpoint that handles messages to four concurrent messages, unless the endpoint sets its own limit:
public sealed class EndpointConcurrencyConvention : IReceiveEndpointConvention
{
public void Configure(IMessagingConfigurationContext context, ReceiveEndpointConfiguration configuration)
{
if (configuration is { Kind: ReceiveEndpointKind.Default, MaxConcurrency: null })
{
configuration.MaxConcurrency = 4;
}
}
}Register a convention#
Register a convention on the bus to apply it on every transport:
builder.Services
.AddMessageBus()
.AddConvention(new EndpointConcurrencyConvention())
.AddRabbitMQ();Register it on a transport to apply it only to the endpoints of that transport:
builder.Services
.AddMessageBus()
.AddRabbitMQ(transport => transport.AddConvention(new EndpointConcurrencyConvention()));Message types use only the conventions registered on the bus.
Convention interfaces#
| Interface | Called for | Settings you can change |
|---|---|---|
IMessageTypeConfigurationConvention | Every message type | Identity, DefaultContentType, MessageSerializer |
IReceiveEndpointConvention | Every receive endpoint | MaxConcurrency, AutoProvision, BindMode, ReceiveMiddlewares |
IConfigurationConvention<DispatchEndpointConfiguration> | Every dispatch endpoint | DispatchMiddlewares |
IRabbitMQReceiveEndpointConfigurationConvention, IAzureServiceBusReceiveEndpointConfigurationConvention, IPostgresReceiveEndpointConfigurationConvention, IInMemoryReceiveEndpointConfigurationConvention | Every receive endpoint of that transport | The endpoint settings of that transport |
Order#
Mocha first applies the settings of the single message type or endpoint, such as AddMessage<T>() or an endpoint's MaxConcurrency(...). Then it calls the conventions registered on the bus, in the order you registered them. After those, it assigns the default urn:message: identity to a message type that still has none, and calls the conventions registered on the transport of an endpoint.
A convention that sets a value replaces the setting of the single message type or endpoint. Check that a value is unset before you set it.
Example: message identities#
For a convention that sets the identity of every message type, see How message type resolution works.
Edit this page on GitHub